A compound statement is a brace-delimited sequence of statements treated as one statement. Braces therefore let a function body or control-flow body contain several operations. section 2.1
A loop body as one statement
for (int i = 0; i < 5; ++i) { printf("%d\n", i); record(i); } The compound statement after for is the loop body. The for statement repeats the entire brace-delimited body, not merely its first line, for i values 0 through 4. section 2.4.1
Use compound statements to make control-flow structure explicit:
- Place the repeated operations of a for loop inside { ... }. section 2.4.1
- Define iteration variables in the initial clause of the for statement so their scope stays narrow. section 2.4.1
- Use the same compound-statement form for while and do loop bodies when several statements must execute together. 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);
}
Compare compound loop bodies with scoped iteration variables across several for forms. section 3.2
