An expression such as x = 0 or i++ or printf(...) becomes a statement when it is followed by a semicolon. Braces { and } are used to group declarations and statements together into a compound statement, or block, so that they are syntactically equivalent to a single statement.
string str = "foo"; // is a statement
// below is a code block
{
int i = 100;
str = "foo2";
}
Conditional Statements:
if-else: if ( expression ) statement1 [else statement2]
Examples:
int i=1, j=2;
string str1 = "One";
string str2 = "Two";
if ( i >j) i = j; // if the value identified by i is greater than the value identified by j assign j to i
if ( (i ==j) || (str2 != str1))
// if i is equal to j or str2 is equal to str1 perform the code block
delimited by brackets.
{
j = j + i;
cout << "j = " << j << " i = "
<< i << endl;
}
if (str1.size() > 2 )
{
...
}
else
{
str1 = "1";
str2 = "2";
}
switch: switch ( expression ) { case const-expr : statements [ case const-expr : statements | default : statements ] }
Examples:
enum SHAPE { CIRCLE, SQUARE, RECTANGLE };
SHAPE shape = SQUARE;
switch (shape)
{
case CIRCLE :
cout << " Area is
determined by PI multiplied by the radius squared" << endl;
break;
case SQUARE:
cout << " The length
and the width are equal for a square." << endl;
case RECTANGLE:
cout << " Area is determined
multiplying the length times the width." << endl;
break;
default :
cerr << " Invalid
shape: Unable to determine the formula for its area. " << endl;
} // end switch
char ch = 'E';
bool exit_program = false;
switch(ch)
{
case 'E' :
cout << "Ending Program
" << endl;
exit_program = true;
break;
default :
cout << " ch = " <<
ch << endl;
} // end switch
Looping Statements
while: while ( expression ) statement
Examples:
while (true)
{
DispatchEvent();
Sleep(5);
}
int arr_int[3] = { 1, 2, 3 };
int i = 0;
while (i < 3)
cout << arr_int[i] << endl;
do-while: do statement while ( expression )
Examples:
char ch;
do
{
cout << "Enter a letter or Q to quit." <<
endl;
cin >> ch
} while (ch != 'Q');
for: for ( expression; conditional-expression; expression )
char ch_array[5] = { 'a', 'b', 'c', 'd', 'e' };
for (int i = 0; i < 5; i++)
cout << ch_array[i] << endl;
for (;true;)
{
DispatchEvent();
Sleep(5);
}
goto : goto label; label:
Example:
bool in_error = true;
if (in_error)
goto HandleError;
int j = j + 1;
HandleError:
cerr << " An error
was encountered." << endl;
break and continue:
The break statement when executed in an if, while, do-while, or switch
statement causes immediate exit from the statement and processing continues
with the first statement after if, while, if-else, or switch statement.
The continue statement causes the program to skip the remaining statements
in a while, do-while, or for loop and proceeds with the next iteration.
int arr[10]; // Defines an array of 10 integers
for (int i =0; i < 10; i++) cout << arr[i] << endl;
Arrays can be intitialize by setting the array equal to the array individual values place in clury braces. When arrays are initialized in this manner the initialization is done at comiple instead of runtime.
double arr_db[3] = { 1.23, 3.5e-10, -10 }
It is considered good practfice to specifiy the size as a const integer. Note by convention all const identifiers are placed in capital letter.
const int BUF_SIZE=80;
char msg[BUF_SIZE];
memset(msg, 0x00, BUF_SIZE); //Initializes
the memory to null terminators.
As stated earlyer arrays are automatic storage types. So in the below example each time foo is called the array err_msg is allocated.
foo()
{
char err_msg[BUF_SIZE];
...
...
}
To keep err_msg from being allocated each time specify it as static.
foo()
{
static char err_msg[BUF_SIZE];
...
...
}
The draw back to this is each function call will use the same memory
location and what ever data is their will be used for subsquent calls of
the function.
So in the example below the second call to foo will cause i to go past
the end of the array.
foo()
{
static int index = 0;
static char err_msg[BUF_SIZE];
for (i=0; i < BUF_SIZE, i++) cout << i
<< endl;
}
It is my advice is that you should not declare static variables in code
blocks unless they are const. If they are not const you can have issues
with concurrancy in multithreaded programs.
There are four ways to pass data int a function: by-value, by-reference-value, by-reference-pointer and by array.
When passing by-value the value passed in is not modified. On the con tray by-reference-value, by-refernce-point and by array the function can modify the original value. To prevent modifications when passing by value use the const modifier.