C++ provides three types of repetition statements (also called looping statements or loops) for performing statements repeatedly while a condition (called the loop-continuation condition) remains true. These are the while, do...while and for statements. (Chapter 5 presents the do
...while
and for
statements, and Chapter 7 presents a specialized version of the for
statement that’s used with arrays and containers.) The while
and for
statements perform the action (or group of actions) in their bodies zero or more times—if the loop-continuation condition is initially false, the action (or group of actions) will not execute. The do
...while
statement performs the action (or group of actions) in its body at least once.
Each of the words if
, else
, switch
, while
, do
and for
is a C++ keyword. Keywords cannot be used as identifiers, such as variable names, and must be spelled with only lowercase letters. Figure 4.2 provides a complete list of C++ keywords.