Loop example in Java


Levels of difficulty: / perform operation:

Java for loop used to repeat execution of statement(s) until a certain condition holds true. for is a keyword in Java programming language.

for (/* Initialization of variables */ ; /*Condition to test*/ ; /* Increment or decrement of variables */) {
  // Statements to execute i.e. Body of for loop
}

Please note that all three components of for loop are optional. For example following for loop prints “Java programmer” indefinitely.

// Infinite for loop
for (;;) {
  System.out.println("Java programmer");
}

Simple for loop example in Java

//Java for loop program
class ForLoop {
  public static void main(String[] args) {
    int c;
 
    for (c = 1; c <= 10; c++) {
      System.out.println(c);
    }
  }
}