O'Reilly logo

Java™ How to Program, Seventh Edition by P. J. Deitel - Deitel & Associates, Inc., H. M. Deitel - Deitel & Associates, Inc.

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.3. for Repetition Statement

Section 5.2 presented the essentials of counter-controlled repetition. The while statement can be used to implement any counter-controlled loop. Java also provides the for repetition statement, which specifies the counter-controlled-repetition details in a single line of code. Figure 5.2 reimplements the application of Fig. 5.1 using for.

Figure 5.2. Counter-controlled repetition with the for repetition statement.
 1 // Fig. 5.2: ForCounter.java
 2 // Counter-controlled repetition with the for repetition statement.
 3
 4 public class ForCounter
 5 {
 6    public static void main( String args[] )
 7    {
 8       // for statement header includes initialization, 
 9       // loop-continuation condition and increment     
10       for ( int counter = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required