codegurukool.com · Free coding tutorials & online compiler

Home › Java Tutorial › Java for loop

Java for loop

Count, or visit every item.

The classic for has three parts: start, condition and step. The for-each loop (for (String n : names)) visits every item of an array or collection without an index.

Example code

public class Main {
  public static void main(String[] args) {
    for (int i = 0; i < 3; i++) {
      System.out.println("count " + i);
    }
    String[] names = {"Ann", "Bo"};
    for (String n : names) {
      System.out.println(n);
    }
  }
}

Run this code in the online editor

← Java while loopJava break and continue →