liveBook · Manning

Loop

As explained in Modern C

A for loop is a C iteration construct that repeatedly executes a statement while controlling initialization, continuation, and update expressions.

```c for (int i = 0; i < 5; ++i) { printf("%d\n", i); } ``` This prints five lines, using `i` as 0, 1, 2, 3, and 4. A `for` loop repeats its body while its condition remains true, combining initialization, continuation, and update in one construct. section 2.4.1

Read `for (clause1; condition2; expression3) statement-or-block` in execution order:

  • Initialize the loop variable once before the first body execution; normally define it in the initial clause so its scope stays narrow. section 2.4.1 section 3.2
  • Check the condition before every body execution; a false value stops the loop. section 2.4.1 section 3.4
  • Execute the body, then evaluate the update expression, such as `++i`, before checking the condition again. section 2.4.1 section 3.2
  • Use `for` for domain iteration; use `while` or `do` when their condition placement better matches the task. section 2.4.1 section 3.2
   for (clause1; condition2; expression3) statement-or-block


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":3},{\"line\":0,\"ch\":6}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":6}]]"}
!@%STYLE%@!

Use the formal pattern to distinguish the initialization clause, continuation condition, update expression, and repeated statement or block. section 3.2

Counting, bounds, and termination

These loops show common variations: ```c for (size_t i = 10; i; --i) { something(i); } for (size_t i = 0, stop = upper_bound(); i < stop; ++i) { something_else(i); } ``` The first counts down while `i` is nonzero. The second keeps the stopping bound in a separate variable. A loop can also omit clauses, as in `for (;;)`, and terminate explicitly with `break`; `continue` skips to the next iteration. section 3.2

How Modern C covers it

How deep it goes
Defines syntax, evaluation order, scoping advice, variants, and termination, while deferring size-type details.
Examples
Uses bounded counting, countdowns, dynamic stopping bounds, infinite loops, and Heron approximation.
What it assumes
Assumes basic C declarations, expressions, blocks, and Boolean-style condition evaluation.
What you can do after
Build it from scratch by selecting initialization, condition, update, and body clauses for repeated C tasks.
How it connects
Connects iteration with loop variables, scopes, conditions, while loops, do loops, break, and continue.

Also explained in

Compare how 27 books explain loop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.