Previous Next

Java For Loop

A for loop is employed in order to repeat a block of code a specific number of times.

Syntax:

for (initialization; testExpression; increment/decrement) {
   // block of code
}

Example:

public class Main {
  public static void main(String[] args) {
    for (int i = 1; i <= 5; i++) {
      System.out.println("Hello Java");
    }
  }
}

Output:

Hello Java
Hello Java
Hello Java
Hello Java
Hello Java
Previous Next