C CLP study materials
C CLP study resources

C CLP: Control Flow and Statements

C CLP · Work through your exam, one topic at a time.

All guide sections

Control Flow and Statements

This domain checks that you can trace control flow through if, switch, and the three loop forms, including fallthrough, the dangling else, and where break and continue actually transfer control.

Selection statements

  • An else binds to the nearest unmatched if; braces make the intent explicit.
  • switch (expr) requires an integer expression after promotion. Every case label must be an integer constant expression, unique within the switch; default may appear anywhere but only once.
  • Falling off the end of one case continues into the next label. break leaves the whole switch; continue inside a switch that sits inside a loop jumps to the loop's next iteration.

Iteration statements and jumps

  • while (cond) body tests first; do body while (cond); tests last, always runs at least once, and needs the trailing semicolon.
  • for (init; cond; step) allows a declaration in init (C99), scoped to the loop; continue still executes step before re-testing. for (;;) loops forever.
  • break exits the innermost loop or switch; goto label jumps within the same function only and may not jump into the scope of a variable-length array.

Worked example

int n = 0, x = 1;
switch (x) {
  case 1: n += 1;
  case 2: n += 2; break;
  case 3: n += 4;
}

x == 1 matches case 1, adds 1, falls through into case 2, adds 2, then break exits. n is 3; case 3 never runs. With x == 3 the result would be 4, and with x == 7 it would be 0 because no label matches and there is no default.

Traps

  • A missing break where fallthrough was not intended, or a switch over an enum with no default.
  • Using a const int variable or a runtime value as a case label - only constant expressions are allowed.
  • A declaration cannot directly follow a case label in C before C23; wrap the case body in braces.
  • A stray ; after while (...) creates an infinite loop with an empty body.
  • while (c = getchar()) stops at the first NUL byte, not at end of file; compare against EOF instead.