C/Flow Control

< C

The C standard defines the following control structures:

Selection

if..else

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

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:

Iteration

for

The for statement has the format:

for (variables; condition; step)
    statement;


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 (;).

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


while

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 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.

do..while

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 C/Flow Control Next: Functions and Methods


Go to the School of Computer Science
This article is issued from Wikiversity - version of the Wednesday, May 13, 2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.