liveBook · Manning

Loop

As explained in Modern C, Third Edition

A for statement is a C iteration statement that controls repeated execution using initialization, a continuation condition, and an iteration expression.

 for (size_t i = 0; i < 5; ++i) {                     
     printf("element %zu is %g, \tits square is %g\n", 
            i,
            A[i],
            A[i]*A[i]);                                
  }                                                    

  return EXIT_SUCCESS;                                 

This `for` statement prints five array elements and their squares. Its counter takes the values 0 through 4. section 2.4 section 2.4.1

A `for` statement has the form `for (clause1; condition2; expression3) body`. The first clause runs once. The condition is checked before each execution of the body; if it is false, the loop stops. The third expression runs after each iteration. A body enclosed in `{ ... }` is a compound statement. section 2.4.1 section 3.2

TAKEAWAY: Use `for` when iterating over a domain, and make the counter's role easy to see.

  • Define the iteration variable in the first clause rather than reusing a variable from outside the loop. section 2.4.1
  • Usually enclose the body in `{ ... }`; the four-part statement can otherwise be difficult to scan. section 3.2
  • The condition need not compare explicitly with zero: `for (size_t i = 10; i; --i)` runs with `i` from 10 down to 1. section 3.2
for (size_t i = 10; i; --i) {
  something(i);
}
for (size_t i = 0, stop = upper_bound(); i < stop; ++i) {
  something_else(i);
}
for (size_t i = 9; i <= 9; --i) {
  something_else(i);
}

These loops show a countdown, a bound computed once in the first clause, and a `size_t` countdown whose condition also stops the loop after zero. section 3.2

`for` is not limited to fixed ranges. `for (;;)`, with no continuation condition, can use `break` to stop when a result is close enough. `continue` can skip the rest of a body while the loop retains its iteration limit. For iterations governed chiefly by a condition rather than a domain, C also provides `while` and `do`. section 3.2 section 3.4

Termination and progress are separate concerns. A loop that repeatedly visits a finite set of states without observable side effects has failed; an apparently unbounded loop may nevertheless perform I/O, change global state, or terminate execution. section 15.4

How Modern C, Third Edition covers it

How deep it goes
Explains clause order, condition checks, counter scope, countdowns, and `break` and `continue`, then examines progress without deriving compiler internals.
Examples
Shows array output, three counter variations, and a bounded numerical approximation loop.
What it assumes
Assumes familiarity with C declarations, expressions, and compound statements while explaining the `for` statement's execution order.
What you can do after
Practice/perform domain iteration with a locally declared counter, and check a loop's termination and observable progress.
How it connects
Connects loops to compound statements, conditions, `while`, `do`, `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, Third Edition. The section links point to the parts of the book it draws on.