5.2. Essentials of Counter-Controlled Repetition

This section uses the while repetition statement to formalize the elements required to perform counter-controlled repetition:

1. the name of a control variable (or loop counter)

2. the initial value of the control variable

3. the loop-continuation condition that tests for the final value of the control variable (i.e., whether looping should continue)

4. the increment (or decrement) by which the control variable is modified each time through the loop.

The program in Fig. 5.1 prints the numbers from 1 to 10. The declaration in line 8 names the control variable (counter), declares it to be an unsigned int, reserves space for it in memory and sets it to an initial value of 1. Declarations that require ...

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.