Section Navigation

Program Flow

The flow of the program can be controled with flow-control statements, which are always contected to some branching conditions of boolean type. Typically the boolean value is the results from one of the following comparison operators:

In addition several comparison can be combined with the boolean operation && (and), || (or) and ! (not). Note that the names for the branching statements are keywords of the C language and cannot be used as variable names.

The if and if-else Statement

The if statement executes the following block of statements, when the branching condition is true, or does nothing for a false result. A variation is the if-else statement, where the block after the else keyword is executed, when the branching condition is false.

Example:

if (a!=0) {
b=2./a;
}

This is a useful statement to avoid the run time error of a division by zero. To give an error message, when a is zero the if-statement is extended to an if-else statement.

if (a!=0) {
b=2./a;
}
else {
printf("Error: division by zero\n");
}

The for Statement

Simple loops are done with the for-statement. Besides the branching condition, which is here the stopping condition, the for-loop allows also defines a variable, which is automatically incremented at each new start of the loop. The syntax of the loop definition is in between a pair of round brackets, supplying the initial value, the stopping condition and the increment operation, each separated by two semi-colons. The body of the loop follows in the statement block, marked by curly brackets.

Example the factorial of n.

fac=1;
for (i=1; i <= n; i++){
fac=fac*i;
}

Note that the operation i++ is a very useful abreviation to increment a number by one. It is absolutely identical to the statement i=i+1;. The loop uses the variable i to count from one to n. At each execution of the loop the value of i is multiplied to the variable fac, which has been initialized to 1 before the loop.

The while Statement

The while-statement is a slimed-down version of the for-statement, because it misses the initialization and increment operation in the definition. It is important to supply code in the following code block to allow the branching condition to become false. The while statement is frequently use for processing user input, e.g. executing some commands, till the user types 'q' for quitting.

Example:

while (i > 0) {
i=i-1;
printf("The variable i is still a positive number\n");
}

Here the value of the variable is decremented by one for each loop. As long the number is positive the message The variable i is still a positive number will be printed on the screen. Note that without the decrement operation there is not a chance that i would ever become negative. The loop would be executed forever, filling the screen over and over with the same message.

The do Statement

The do-statement is very similar to the while-statement, with the difference that the loop is executed before the stopping condition is evaluated.

do {
i++;
} while (i < 10);

Note that the loop content is between the keywords do and while, with the branching condition at the end. Note that the semi-colon at the end is essential, while the other control-flow statements (if, while, for) can just end with the curly bracket.

The break and continue Statement

Sometimes it is necessary to jump over the remaining part of a statement block or even exit it. These can be done with the break- and continue-statement. They act as a single command and must be terminated with a semi-colon. The break-steatement exit the inner most block, while the continue-statement jumps to the end. They are useful to catch and handle some situation, which might cause an error or even crashes the program.

Example:

for (i=-5; i < 6; i++){
if (i==0) { continue; }
inv=1./i;
}

The index is running from -5 to 5 in increments of 1 and calculates the inverse. The if-statement checks against a zero value and than skip the remaining lines to avoid the division by zero error.

The switch Statement

The switch statement is a very handy statement, when the content of a variable has to be checked against multiple value. Technically the construct can be rewritten with the if-statement, checking agains equal values.

Example (c is of type char, holding a single character):

switch (c) {
case 'a': printf("c holds the lower case A\n");
break;
case 'A': printf("c holds the upper case A\n");
break;
default: printf("c does not hold an A at all\n");
}

The switch-statement has a somehow poor syntax, which requires the break-statement at end of each case. If it is missing all other following cases are executed as well, which can be used by advanced programmer as an advantage as seen in this example.

switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': printf("c holds a vowel\n");
break;
default: printf("c does not hold a vowel\n");
}

previous:next