C/Flow Control

From Wikiversity
< C
Jump to navigation Jump to search

Objective[edit]

Books-aj.svg aj ashton 01f.png
  • Familiarise yourself with control structures.
  • Learn how to use if...else statements.
  • Learn how to use the switch statement.
  • Learn how to use the while loop.
  • Learn how to use the do...while loop.
  • Learn how to use the for loop.

Lesson[edit]

Branching[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
}

If a branch contains only one statement, the {} are optional. You can use an if without an accompanying else.

An example of if...else in action:

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

Iteration[edit]

for[edit]

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. If the loop contains only one statement, the {} are optional. Here is an example of the for loop in action:

int i;
for (i = 1; i <= 10; i++)
    printf("i = %d\n", i);

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[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. If the loop contains only one statement, the {} are optional.


do...while[edit]

This is the format of the do iteration statement:

do {
    statement
} while (expression);

statement is executed repeatedly as long as expression evaluates to anything but zero. If the loop contains only one statement, the {} are optional. Here is an example of a do...while loop in action:

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.

Assignments[edit]

Crystal Clear app kedit.svg

Icon tools.svg Completion status: Almost complete, but you can help make it more thorough.