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.

 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 = 1

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.