C/Flow Control

From Wikiversity
< C
Jump to: navigation, search

The C standard defines the following control structures:



This structure lets the program perform one of two actions, depending on the value of an expression. Its basic structure is:

if (expression) {
} else {

Multiple statements can be used if they are enclosed by braces, e.g:

if (n==0){
} 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')
else if (a=='f')
else if (a=='q')
    printf("Unknown operation %c\n", a);


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':
    case 'f':
    case 'q':
        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 either int or unsigned int.
  • The break statement ensures execution does not fall through to the next statement.
  • Control passes to the default label if none of the other case constants match the value of the control expression; if there is no default statement, none of the sub-statement of the switch is executed.



The for statement has the format:

for (variables; condition; step)

variables is usually variable initialization; 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 of code, which is a sequence of statements enclosed by braces. variables, condition, and step can be omitted since these are optional. When condition is omitted, the condition is assumed permanently true. Although you can omit parts of a for statement, you'll still need to separate the omitted parts with a semicolon (;).

    printf("This is an infinite loop!\n");


A while loop has the following format:

while (expression)

expression is evaluated. If it is true (non-zero) the body of the loop, the statement, is executed; expression is then reevaluated 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.


This is the format of the do iteration 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 — C/Flow Control — Next: Functions and Methods

Go to the School of Computer Science