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


  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.

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
 
 1 2 3 4 5 6 7 8 9 10
 

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.


Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net