Skip to main content
Logo image

Section 4.7 Control Flow

C provides several language constructs to control the program flow. These determine which statements are executed and when they are executed.

Subsection 4.7.1 Selection Statements

The if-statement if (C) A else B evaluates the condition C. If its value is unequal to 0, statement A is executed, otherwise B:
void print_max(int a, int b) {
    if (a < b)
        printf("%d\n", b);
    else
        printf("%d\n", a);
}
Run
Listing 4.7.1. Example program using an if-statement
Another selection statement is the switch-statement. It evaluates an expression of type int and jumps to different case labels depending on the result:
enum { jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec };

int days_per_month(unsigned month, bool is_leap_year) {
    assert (month < 12);
    switch (month) {
    case jan:
    case mar:
    case may:
    case jul:
    case aug:
    case oct:
    case dec:
        return 31;
    case feb:
        return 28 + is_leap_year;
    default:
        return 30;
    }
}
Run
Listing 4.7.2. Example program using a switch-statement with an enumeration type. The assert statement in the first line of the function body validates when executed that the function is called with a number corresponding to a valid month and aborts the execution if that is not the case.
If the selecting value fits to no provided case label, execution jumps to the default label. It is important to note that after execution jumps to a label, all statements until the end of the switch block are executed. If this behavior is undesired, the switch block can be left earlier with a break; or a return statement.

Subsection 4.7.2 Loops

C has three kinds of loops. The do-while-loop do A while (C) executes the statement A and then checks the condition C to determine if the loop needs to be executed again:
int sum(void) {
    int sum = 0;
    int n;
    do {
        printf("Enter a number (0 = finish): ");
        scanf("%d", &n);
        sum += n;
    } while (n != 0);
    printf("The sum is %d\n", sum);
}
Run
Listing 4.7.3. Example program using a do-while-loop
Characteristically, the loop body A is always executed at least once, independently of C.
The while-loop while (C) A repeats A as long as the condition c is non-zero when it is checked before an iteration. In contrast to the do-while-loop, A is never executed if C is not satisfied before executing the loop.
int sum(int *a, int n) {
    int res = 0;
    int i = 0;
    while (i < n) {
        res += a[i];
        i = i + 1;
    }
    return res;
}
Run
Listing 4.7.4. Example program using a while-loop
The for-loop is an abbreviation of a while-loop to compactly declare iteration variables:
int sum(int *a, int n) {
    int res = 0;
    for (int i = 0; i < n; i++)
        res += a[i];
    return res;
}
Run
Listing 4.7.5. Example program using a for-loop
The break-statement can be used to leave the innermost surrounding loop:
void sum(void) {
    int sum = 0;
    for (;;) {  // is equal to while (true)
        int n;
        printf("Enter a number (0 = finish): ");
        scanf("%d", &n);
        if (n == 0)
            break;
        sum += n;
    }
    printf("The sum is %d\n", sum);
}
Run
Listing 4.7.6. Example program using a break-statement. scanf is a function to query for inputs from the user.
The continue-statement jumps directly to the end of the loop body. For do-while and while-loops, this means that the termination condition is checked in the next step, in for-loops the increment portion of the loop is executed first.
void cycle(int mod) {
    for (int i = 0; ; i++) {
        if (i == mod) {
            i = 0;
            continue;
        }
        printf("%d\n", i);
    }
}
Run
Listing 4.7.7. Example program using a continue-statement

Subsection 4.7.3 Function Return

The return-statement, as we have seen before, ends the execution of the current function and returns the program flow to the calling function. Unless the function has a void return type, the return-statement requires a return value:
int max(int a, int b) {
    if (a < b)
        return b;
    else
        return a;
}
Run
Listing 4.7.8. Example program using a return-statement