Essentials of Counter-Controlled Repetition
This section uses the while repetition statement introduced in Chapter 4 to formalize the elements required to perform counter-controlled repetition. Counter-controlled repetition requires
- the name of a control variable (or loop counter)
- the initial value of the control variable
- the loop-continuation condition that tests for the final value of the control variable (i.e., whether looping should continue)
- the increment (or decrement) by which the control variable is modified each time through the loop.
Consider the simple program in Fig. 5.1, which prints the numbers from 1 to 10. The declaration at line 9 names the control variable (counter), declares it to be an integer, reserves space for it in memory and sets it to an initial value of 1. Declarations that require initialization are, in effect, executable statements. In C++, it is more precise to call a declaration that also reserves memoryas the preceding declaration doesa definition. Because definitions are declarations, too, we will use the term "declaration" except when the distinction is important.
Figure 5.1. Counter-controlled repetition.
1 // Fig. 5.1: fig05_01.cpp 2 // Counter-controlled repetition. 3 #include 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int counter = 1; // declare and initialize control variable 10 11 while ( counter <= 10 ) // loop-continuation condition 12 { 13 cout << counter << " "; 14 counter++; // increment control variable by 1 15 } // end while 16 17 cout << endl; // output a newline 18 return 0; // successful termination 19 } // end main
|
The declaration and initialization of counter (line 9) also could have been accomplished with the statements
int counter; // declare control variable counter = 1; // initialize control variable to 1
We use both methods of initializing variables.
Line 14 increments the loop counter by 1 each time the loop's body is performed. The loop-continuation condition (line 11) in the while statement determines whether the value of the control variable is less than or equal to 10 (the final value for which the condition is TRue). Note that the body of this while executes even when the control variable is 10. The loop terminates when the control variable is greater than 10 (i.e., when counter becomes 11).
Figure 5.1 can be made more concise by initializing counter to 0 and by replacing the while statement with
while ( ++counter <= 10 ) // loop-continuation condition cout << counter << " ";
This code saves a statement, because the incrementing is done directly in the while condition before the condition is tested. Also, the code eliminates the braces around the body of the while, because the while now contains only one statement. Coding in such a condensed fashion takes some practice and can lead to programs that are more difficult to read, debug, modify and maintain.
Common Programming Error 5.1
Floating-point values are approximate, so controlling counting loops with floating-point variables can result in imprecise counter values and inaccurate tests for termination. |
Error-Prevention Tip 5.1
Control counting loops with integer values. |
Good Programming Practice 5.1
Put a blank line before and after each control statement to make it stand out in the program. |
Good Programming Practice 5.2
Too many levels of nesting can make a program difficult to understand. As a rule, try to avoid using more than three levels of indentation. |
Good Programming Practice 5.3
Vertical spacing above and below control statements and indentation of the bodies of control statements within the control statement headers give programs a two-dimensional appearance that greatly improves readability. |