C Programming/Flow Control

From Wikiversity
(Redirected from C/Flow Control)
Jump to navigation Jump to search
Completion status: Almost complete, but you can help make it more thorough.

Objective[edit | edit source]

  • 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 | edit source]

Branching[edit | edit source]

if ... else[edit | edit source]

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

if (expression) {
    statements ...
} else {
    statements ...
}

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 (x == true) {
    printf("True");
} else {
    printf("False");
}

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 | edit source]

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 | edit source]

for loops[edit | edit source]

The for statement has the format:

for(variables; condition; step)
{
    statements ...
}

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 loops[edit | edit source]

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 loops[edit | edit source]

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 | edit source]