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 initialization are executable statements. In C++, it’s more precise to call a variable declaration that also reserves memory a definition. Because definitions are declarations, too, we’ll use the term “declaration” except when the distinction is important.


 1   // Fig. 5.1: fig05_01.cpp
 2   // Counter-controlled repetition.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main()
 7   {
 8      unsigned int counter = 1; // declare and initialize control variable
 9
10      while ( counter <= 10 ) // loop-continuation condition
11      {
12         cout << counter << " ";
13         ++counter; // increment control variable by 1
14      } // end while
15
16      cout << endl; // output a newline
17   } // end main


1 2 3 4 5 6 7 8 9 10


Fig. 5.1. Counter-controlled repetition.

Line 13 increments the loop counter by 1 each time the loop’s body is performed. The loop-continuation condition (line 10) 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). 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 is 11).

Figure 5.1 can be made more concise by initializing counter to 0 and by replacing the while statement with

counter = 0;
while ( ++counter <= 10 ) // loop-continuation condition
   cout << counter << " ";

This code saves a statement, because the incrementing is done 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 can lead to programs that are more difficult to read, debug, modify and maintain.


Image Error-Prevention Tip 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. Control counting loops with integer values. Separately, ++ and -- can be used only with integer operands.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.199.14