Essentials of Counter-Controlled Repetition
This section uses the while repetition statement introduced in Chapter 5 to formalize the elements required to perform counter-controlled repetition. Counter-controlled repetition requires
- a control variable (or loop counter)
- the initial value of the control variable
- the increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop)
- the loop-continuation condition that determines whether looping should continue.
To see these elements of counter-controlled repetition, consider the application of Fig. 6.1, which uses a loop to display the numbers from 1 through 10. Note that Fig. 6.1 contains only one method, Main, which does all of the class's work. For most applications in Chapters 4 and 5, we have encouraged the use of two separate filesone that declares a reusable class (e.g., Account) and one that instantiates one or more objects of that class (e.g., AccountTest) and demonstrates their functionality. Occasionally, however, it is more appropriate simply to create one class whose Main method concisely illustrates a basic concept. Throughout this chapter, we use several one-class examples like Fig. 6.1 to demonstrate the mechanics of various C# control statements.
Figure 6.1. Counter-controlled repetition with the while repetition statement.
1 // Fig. 6.1: WhileCounter.cs 2 // Counter-controlled repetition with the while repetition statement. 3 using System; 4 5 public class WhileCounter 6 { 7 public static void Main( string[] args ) 8 { 9 int counter = 1; // declare and initialize control variable 10 11 while ( counter <= 10 ) // loop-continuation condition 12 { 13 Console.Write( "{0} ", counter ); 14 counter++; // increment control variable 15 } // end while 16 17 Console.WriteLine(); // output a newline 18 } // end Main 19 } // end class WhileCounter
|
In method Main of Fig. 6.1 (lines 718), the elements of counter-controlled repetition are defined in lines 9, 11 and 14. Line 9 declares the control variable (counter) as an int, reserves space for it in memory and sets its initial value to 1.
Line 13 in the while statement displays control variable counter's value during each iteration of the loop. Line 14 increments the control variable by 1 for each iteration of the loop. The loop-continuation condition in the while (line 11) tests 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 application performs the body of this while even when the control variable is 10. The loop terminates when the control variable exceeds 10 (i.e., counter becomes 11).
|
|
|
The application in Fig. 6.1 can be made more concise by initializing counter to 0 in line 9 and incrementing counter in the while condition with the prefix increment operator as follows:
while ( ++counter <= 10 ) // loop-continuation condition Console.Write( "{0} ", counter );
This code saves a statement (and eliminates the need for braces around the loop's body), because the while condition performs the increment before testing the condition. (Recall from Section 5.12 that the precedence of ++ is higher than that of <=.) Coding in such a condensed fashion might make code more difficult to read, debug, modify and maintain.
|