Java · Lesson 3 of 3

🔁 Loops in Java

Repeat code with for and while.

A for loop has three parts: start, condition and step.

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

💡 i++ means add 1 to i.

✏️ Your turn

Open this example in the playground, change it and run it.

▶ Try it yourself

🧠 Quick quiz

What does i++ do?