for Loop

Java loops are used to iterate a block of code multiple number of times using for, while or do while loop.

Java for loop iterates the block of code for the fixed number of times. It should be used if number of iterations is known before.


for (initializer, condition, iteration)
    // code to be executed

The for loop requires following three parts:

  1. Initializer: It initialize a counter variable with a starting value.
  2. Condition: It tests the condition that must evaluate to true for next iteration to be executed.
  3. Iteration: It increase or decrease the counter.


for ( int i = 0; i < 5; i++ )
    System.out.print( i + “  ” );


0 1 2 3 4