In a counter-controlled loop, you can only decrement the counter variable.

Typically, for statements are used for counter-controlled repetition and while statements for sentinel-controlled repetition.

If the initialization expression in the for header declares the control variable, the control variable can be used only in that for statement.

A variable�s scope defines where it can be used in a program.
A local variable can be used only in the method that declares it and only from the point of declaration through the end of the method.

The increment expression in a for acts as if it were a standalone statement at the end of the for�s body, so

This section uses the while repetition statement introduced in to formalize the elements required to perform counter-controlled repetition. Counter-controlled repetition requires

  1. a control variable (or loop counter)
  2. the initial value of the control variable
  3. the increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop)
  4. the loop-continuation condition that determines whether looping should continue.

To see these elements of counter-controlled repetition, consider the application of , which uses a loop to display the numbers from 1 through 10. Note that contains only one method, main, which does all of the class's work. For most applications in , 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 its (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 to demonstrate the mechanics of Java's control statements.

Example 5.1. Counter-controlled repetition with the while repetition statement.

 1 // Example 5.1: WhileCounter.java
 2 // Counter-controlled repetition with the while repetition statement.
 3
 4 public class WhileCounter
 5 {
 6 public static void main( String args[] )
 7 {
 8 int counter = 1; // declare and initialize control variable
 9
10 while ( counter <= 10 ) // loop-continuation condition
11 {
12 System.out.printf( "%d ", counter );
13 ++counter; // increment control variable by 1
14 } // end while
15
16 System.out.println(); // output a newline
17 } // end main
18 } // end class WhileCounter

1 2 3 4 5 6 7 8 9 10


In main of (lines 617), the elements of counter-controlled repetition are defined in lines 8, 10 and 13. Line 8 declares the control variable (counter) as an int, reserves space for it in memory and sets its initial value to 1. Variable counter could also have been declared and initialized with the following local-variable declaration and assignment statements:

 int counter; // declare counter
 counter = 1 ; // initialize counter to 1

Line 12 in the while statement displays control variable counter's value during each iteration of the loop. Line 13 increments the control variable by 1 for each iteration of the loop. The loop-continuation condition in the while (line 10) 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 program 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).

Common Programming Error 5.1

In a counter-controlled loop, you can only decrement the counter variable.

Because floating-point values may be approximate, controlling loops with floating-point variables may result in imprecise counter values and inaccurate termination tests.


Error-Prevention Tip 5.1

In a counter-controlled loop, you can only decrement the counter variable.

Control counting loops with integers.


Good Programming Practice 5.1

In a counter-controlled loop, you can only decrement the counter variable.

Place blank lines above and below repetition and selection control statements, and indent the statement bodies to enhance readability.

The program in can be made more concise by initializing counter to 0 in line 8 and preincrementing counter in the while condition as follows:

 while ( ++counter <= 10 ) // loop-continuation condition
 System.out.printf( "%d ", 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 that the precedence of ++ is higher than that of <=.) Coding in such a condensed fashion takes practice and might make code more difficult to read, debug, modify and maintain, and typically should be avoided.

Does a counter

A counter-controlled loop (or counting loop) is a loop whose repetition is managed by a loop control variable whose value represents a count.

What is counter control variable?

The Count-Controlled while loop A counter variable is created and initialized to a starting value before the loop is started. The condition that is tested before each iteration of the loop is whether the counter has reached a predetermined value. The counter itself is incremented inside the while loop.

What is a count

A count-controlled loop is used when the number of iterations to occur is already known. A count-controlled loop is so called because it uses a counter to keep track of how many times the algorithm has iterated.

What should you use as a counter in counter

Typically, for statements are used for counter-controlled repetition and while statements for sentinel-controlled repetition. If the initialization expression in the for header declares the control variable, the control variable can be used only in that for statement.