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


























