... required. If the loop-continuation condition is initially false, the program does not execute the for statement’s body. Instead, execution proceeds with the statement following the for.

Representing a for Statement with an Equivalent while Statement

The for statement often can be represented with an equivalent while statement as follows:


initialization;

while (loopContinuationCondition) {
   statement
   increment;
}

In Section 5.10, we show a case in which a for statement cannot be represented with an equivalent while statement. Typically, for statements are used for counter-controlled iteration and while statements for sentinel-controlled iteration. However, while and for can each be used for either iteration type.

Scope of a for Statement’s Control ...

Get C++ How to Program, 10/e now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.