Java Loops Explained: for, while and do-while (with Examples)
Learn Java loops the easy way — for, while, and do-while with clear examples, break and continue, the for-each loop, and how to avoid infinite loops.
Imagine you need to print "Hello" 100 times. You wouldn't write 100 lines — you'd tell the computer to repeat one line 100 times. That's exactly what loops do: they run a block of code again and again until a condition says stop. Loops are one of the most powerful ideas in programming, and Java gives you three flavours: for, while, and do-while.
The for loop
Use a for loop when you know how many times you want to repeat. It packs three parts into one tidy line:
for (int i = 1; i <= 5; i++) { System.out.println("Count: " + i); }
Those three parts, separated by semicolons, are:
int i = 1— the start: create a counter and set its starting value (runs once, at the beginning).i <= 5— the condition: keep looping while this is true (checked before each round).i++— the update: change the counter after each round (here, add 1).
So this prints Count: 1, 2, 3, 4, 5 and then stops (because when i becomes 6, 6 <= 5 is false). Trace it slowly once by hand — the moment this "click" happens, loops make sense forever.
The while loop
Use a while loop when you don't know the exact count — you just want to keep going as long as a condition holds:
int n = 1; while (n <= 5) { System.out.println("Number: " + n); n++; // don't forget to update, or it loops forever! }
The while checks the condition first, then runs the body. Notice you have to update n yourself inside the loop.
The do-while loop
A do-while is like while, but it checks the condition at the end. That means the body always runs at least once, even if the condition is false from the start:
int i = 10; do { System.out.println("This runs once, guaranteed."); i++; } while (i < 5); // false immediately, but body already ran once
It's perfect for things like menus — "show the menu, then ask again if they didn't choose quit."
break and continue
Two keywords give you extra control inside any loop:
break— exit the loop immediately, right now.continue— skip the rest of this round and jump to the next iteration.
for (int i = 1; i <= 10; i++) { if (i == 6) break; // stop completely at 6 if (i % 2 != 0) continue; // skip odd numbers System.out.println(i); // prints 2, 4 }
The enhanced for loop (for-each)
There's also a simpler for for going through every item in a collection or array — you'll use this constantly once you learn arrays in the next lesson:
int[] scores = {85, 90, 78};
for (int score : scores) { // "for each score in scores"
System.out.println(score);
}
Which loop should I use?
for— you know the number of repetitions (counting 1 to 100, going through a list).while— you loop until some condition changes and don't know the count in advance.do-while— same as while, but the body must run at least once (menus, prompts).
My most common beginner mistake with loops? Forgetting the i++ in a while loop and freezing the whole program. If yours ever hangs, check that the counter is actually changing — nine times out of ten, that's the culprit.
Key takeaways
- Loops repeat a block of code so you don't have to write it out many times.
for= known count (start; condition; update).while= loop while a condition holds.do-while= runs at least once.- Always make sure the loop can end — an unchanging condition means an infinite loop.
breakexits a loop;continueskips to the next round.
← Previous: Lesson 7 — Decisions: if, else & switch
Next: Lesson 9 — Methods (Functions) in Java →