5.3. for Repetition Statement

In addition to while, C++ provides the for repetition statement, which specifies the counter-controlled repetition details in a single line of code. To illustrate the power of for, let’s rewrite the program of Fig. 5.1. The result is shown in Fig. 5.2.

 1   // Fig. 5.2: fig05_02.cpp 2   // Counter-controlled repetition with the for statement. 3   #include <iostream> 4   using namespace std; 5  6   int main() 7   { 8      // for statement header includes initialization,           9      // loop-continuation condition and increment.             10      for ( unsigned int counter = 1; counter <= 10; ++counter )11         cout << counter << " ";                                12 13      cout ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.