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: � greater than: > � less than: < � greater than or equal to: >= � less than or equal to: <= � equal to: == � not equal to: != 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.
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.
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.
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"); }