Flow control in C
The C standard defines the following control structures:
Contents |
Selection [edit]
if..else [edit]
This structure lets the program perform one of two actions, depending on the value of an expression. Its basic structure is:
if (expression)
statement1;
else
statement2;
Multiple statements can be used if they are enclosed by braces, e.g:
if (n==0){
printf("Done.\n");
} else {
hanoi(n-1, source, target, aux);
printf("move from %d to %d.\n", source, target);
hanoi(n-1, aux, source, target);
}
Statement 2 can be an if statement, which can be used to create an if..else if..else structure, e.g:
if (a=='h')
printHelp();
else if (a=='f')
findWord();
else if (a=='q')
exit(0);
else
printf("Unknown operation %c\n", a);
switch..case..default [edit]
The switch statement will go to one of several locations, depending on the value of an expression. The last example can be written using a switch statement as:
switch (a) {
case 'h':
printHelp();
break;
case 'f':
findWord();
break;
case 'q':
exit(0);
break;
default:
printf("Unknown operation %c\n", a);
}
A few notes:
- the control statement (in this example the variable
a) has to have integral type (character, short integer, integer bit-field, all of these signed or not, or an object of enumeration type); integral promotion will ensure the type is eitherintorunsigned int. - The
breakstatement ensures execution does not fall through to the next statement. - Control passes to the
defaultlabel if none of the other case constants match the value of the control expression; if there is nodefaultstatement, none of the substatement of the switch is executed.
Iteration [edit]
for [edit]
The for statement has the format:
for (variables; condition; step )
statement
variables is usually variable initialisation; condition is the condition that keeps the loop active, usually a relational expression; step is usually an increment or decrement of one or more variables. Here is an example:
int i;
for (i = 1; i <= 10; i++)
printf("i = %d\n", i);
statement can be replaced by a block, which is a sequence of statements enclosed by braces. Any of variables, condition, or step can be omitted. When condition is omitted, the condition is assumed permanently true.
while [edit]
A while loop has the following format:
while ( expression )
statement
Expression is evaluated. If it is true (non-zero) the body of the loop, the statement, is executed; expression is then re-evaluated and if true the body is executed again. Only when expression is false (zero) will statement be skipped and the loop terminated.
Statement can be replaced by a block. A block is a sequence of statements enclosed by braces.
do..while [edit]
This is the format of the do iteration statement:
do
statement
while ( expression ) ;
statement is executed repeatedly as long as expression remains unequal to zero. statement can be replaced by a list of statements enclosed by braces (a block) as demonstrated in this example:
do {
printf("Press 'q' to exit: ");
scanf("%s", str);
} while ( str[0] != 'q' && str[0] != 'Q' );
This will loop repeatedly, asking for input, until either 'q' or 'Q' is entered.
Note: a do..while loop is guaranteed to run at least once.
| Project: Topic:C |
| Previous: Variables and Expressions — Flow control in C — Next: Functions and Methods |
| Go to the School of Computer Science |