Skip to chapter content
navan✳THE BUILD LABC17 / GUIDE
0/24 read
CHAPTER 04 / 21 · THE LANGUAGE

Decisions and loops.

Read if, switch, for, while, and loop exits as explicit control flow.

BY NAVAN SAMILONG READC17 BASELINETHEORY FIRST
01 / THE MENTAL MODEL

The idea behind the syntax.

Control flow chooses which statement executes next. An if tests whether an expression compares unequal to zero; C does not require a dedicated Boolean type in the condition. An else belongs to the nearest unmatched if. Braces make the intended block unambiguous and keep a later added line from silently changing control flow. A switch selects among integral case labels and is useful when one value has a small set of named cases.

Loops express repetition with different shapes. while checks its condition before each iteration; do while checks after the body; for groups initialization, condition, and update in one header. None of these guarantees termination. A reader needs to know what state changes each time and what condition will eventually stop the loop. break exits the nearest loop or switch; continue moves to the next loop test or update.

02 / THE RULES

What C guarantees.

A switch case runs onward into the next case unless a break, return, or another control transfer stops it. That fallthrough can be intentional, but an explicit break or comment makes the policy visible. The default label handles values not named by a case. Unlike an enum match in some languages, switch does not automatically prove that every conceptual case has been considered.

A counted for loop commonly starts at zero and continues while an index is below a known length. The condition must be checked against the actual array bound, not a guessed constant. An unsigned index has its own boundary traps when counting downward. The example uses a forward loop and a named length so each access stays within the initialized elements.

03 / UNDER THE SURFACE

Why the detail matters.

A condition does not need to be exactly 1 to count as true. Any scalar value that compares unequal to zero is true in C, and a null pointer compares equal to zero in a pointer context. That flexibility can shorten a guard, but a precise comparison is clearer when zero itself has domain meaning. For example, a count of zero may mean an empty collection rather than failure. Write the condition so the reader can see which case is selected and why.

Loop safety has two separate parts: eventual exit and valid work on every iteration. A loop can terminate and still read one element too far on its final pass. Trace the first iteration, the last valid iteration, and the condition that stops the next one. In a switch, likewise trace whether control reaches a later case label because of intended fallthrough or a missing break. Those boundary traces are more informative than checking only a typical middle value.

04 / CODE, EXPLAINED

See the rule in context.

decisions-and-loops.c
#include <stdio.h>
#include <stddef.h>

int main(void) {
    int scores[] = {4, 7, 9};
    size_t count = sizeof scores / sizeof scores[0];
    int total = 0;
    for (size_t i = 0; i < count; ++i) {
        if (scores[i] >= 5) total += scores[i];
    }
    printf("Passing total: %d\n", total);
    return 0;
}
FOLLOW THE EXECUTION

The array contains three integers. sizeof scores gives the entire array's byte size and sizeof scores[0] gives one element's byte size, so count is 3 in this scope. The loop visits indices 0, 1, and 2. Only 7 and 9 pass the condition, yielding 16. The update ++i occurs after each body; the condition stops the loop before index 3 can be used.

05 / THE BOUNDARY

Where the rule stops.

The sizeof array / sizeof array[0] expression works where the object is truly an array. In a function parameter, an array parameter is adjusted to a pointer, so the same expression would not recover the caller's length.

06 / THE CONNECTION

Keep this model.

Control flow becomes more useful when data has a shape. Arrays provide contiguous elements with a fixed extent; the next chapter explains both their convenience and the exact bound that code must respect.

Finished reading?
Built with curiosity. By Navan Sami. ↗C · Types, memory, and the rules behind the code.