Main program shell
// Comment header |
#include <iostream> |
using namespace std; |
|
int main() { |
// variable
declarations |
|
// statements |
|
return 0 ; |
} |
|
|
Variables and data types
char c; |
// character |
int i; |
// integer |
float f; |
// floating point |
int x = 5; |
// integer, set value |
|
|
|
Operators and expressions
Assignment |
|
x = 5 |
// assignment |
i++ |
// increment |
j-- |
// decrement |
|
|
Arithmetic |
|
value + 32 |
// addition |
test - 3 |
// subtraction |
pi * r |
// multiplication |
32 / num |
// division |
|
|
Logical |
|
&& |
Logical AND |
|| |
Logical OR |
! |
Logical NOT |
|
|
Relational |
|
zz == 44 |
Equal to |
points != 10 |
Not equal to |
x < y |
Less than |
test > 6 |
Greater than |
wt <= et |
Less than or equal to |
rad >= 10 |
Greater than or equal |
|
|
Parentheses |
|
(x+3) * 5 |
Control order of
operations with parens |
|
|
|
|
Decisions (if, else, switch)
Simple if |
if( expr) { |
//
statements |
} |
|
If-then-else |
if( expr) { |
//
statements |
} else if( expr) { |
//
statements |
} else { |
//
statements |
} |
|
Switch |
switch( expr) { |
case
constant: |
// statements |
break; |
case
constant: |
// statements |
break ; |
... |
default: |
// statements |
} |
|
|
Looping
While loop |
while( expr) { |
//
statements |
} |
|
Do-while loop |
do { |
//
statements |
} while( expr); |
|
For loop |
for( initialize-expr;
test-expr; |
update-expr) { |
//
statements |
} |
|
|
Standard input and output
Getting input |
cin
>> value; |
|
Writing output |
cout
<< "Hello world"; |
cout
<< "Number " << x << endl; |
|
|
|