Counting, Packaged

Most loops count: from 1 to n, from 0 to a limit, down from a total. while can express all of them, but the three pieces of a counting loop, start, test, step, end up scattered above and inside the body. for gathers them into one header, and once it clicks, it becomes the loop you write most.

The Header's Three Slots

for (initialization; condition; update) {
    /* body */
}

Execution order is precise and examinable: the initialization runs once; the condition is tested before every pass, exactly like while; the body runs; the update runs after the body; back to the condition. A for is a while with the bookkeeping folded in:

Two ANSI C notes on this shape. The counter is declared at the top of the block, because for (int i = 1; ...), the form later standards allow, is a compile error in this dialect and on exams keyed to it. And after the loop ends, i holds the first value that failed the test, here 6, a fact output-prediction questions lean on.

The Idioms Worth Owning

Counting up from 1 to n, counting from 0 while i < n (the shape that will match arrays perfectly in chapter 7), and counting down: all are one-line variations of the header. The classic exam program, the multiplication table, is the counting-up idiom verbatim:

Accumulating while counting is the other staple: factorial multiplies into a running product, sums add into a running total, and both are a for with one statement of body. Note the type lesson hiding in factorial: 13 factorial already overflows a 32-bit int, a reminder that chapter 2's ranges never stopped applying.

Empty Slots and the Comma

Each header slot is optional. An omitted condition is taken as true, so for (;;) is the idiomatic infinite loop, equal to while (1). Omitting initialization or update moves that work elsewhere, at which point the loop is drifting toward what while says more honestly, a legitimate style call.

The comma operator from chapter 3 finally earns its keep here, running two updates in one slot:

Two counters march toward each other in one header. This two-pointer shape returns in the arrays and strings chapters (reversing, palindromes), so meeting the comma here is an investment.

Two Planted Bugs

Exams and real life share the same two for accidents. A semicolon straight after the header, for (i = 0; i < 5; i++);, is a complete loop with an empty body: the braces that follow run once, after the loop finishes. And an update that fights the condition, i-- where the test is i < n, never terminates. Both compile cleanly; both are found by reading the header aloud: "start here; keep going while this; do this each time".

Which Loop, When

The honest division of labor, worth giving as an exam answer: for when the number of iterations is known or counted; while when it is discovered as you go (sentinels, EOF); do-while when the body must run at least once (menus, digit counting). All three are interchangeable with effort, and the effort is the tell that you picked the wrong one.

Key Takeaways

  • for (init; condition; update): init once, test before each pass, update after each body; equal to the corresponding while.
  • ANSI C declares the counter before the loop; for (int i = ...) is a later-standard form and a compile error here.
  • After the loop, the counter holds the first failing value.
  • for (;;) loops forever; the comma operator legitimately runs paired updates in one slot.
  • A semicolon after the header makes an empty-body loop; an update that fights the condition never ends.
  • Known count: for. Discovered end: while. At least once: do-while.