ProLesson 2.5Beginner3 min readUpdated 30 October 2026
Loops: for, while, do-while, break and continue
Repeat work with for, while and do-while, loop over collections with for-each, and control loops with break and continue.
A loop repeats a block of code. Printing 100 invoices, checking every item in a cart, retrying a payment: all loops.
01The for loop
Use for when you know how many times to repeat:
ForLoop.java
public class ForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Step " + i);
}
}
}Step 1 Step 2 Step 3 Step 4 Step 5
It has three parts: start (int i = 1), condition (i <= 5, checked before each round) and update (i++, after each round).
Loading the rest of the lesson…