Counter-controlled repetition with the for repetition statement :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.
Counter-controlled repetition with the for repetition statement in Java
// Fig. 5.2: ForCounter.java
// Counter-controlled repetition with the for repetition statement.
public class ForCounter
{
public static void main(String[] args)
{
// for statement header includes initialization,
// loop-continuation condition and increment
for (int counter = 1; counter <= 10; counter++)
System.out.printf("%d ", counter);
System.out.println();
}
} // end class ForCounter
Output of Program
1 2 3 4 5 6 7 8 9 10