Loops (for, while)
Use a for loop when you know how many times to repeat. Use a while loop when you need to keep repeating as long as a condition stays true.
A do-while loop always runs at least once, even if the condition is false from the start.
for (int i = 1; i <= 5; i++) {
System.out.println("Lap " + i);
}- for: when you know the count upfront
- while: keeps going while the condition is true
- do-while: runs at least once
A for loop has three parts, separated by semicolons: initialization (runs once, before the loop starts), condition (checked before every iteration — the loop stops the moment it's false), and update (runs *after* every iteration, before the next condition check).
You can initialize/update multiple variables too, separated by commas — like one variable increasing, another decreasing. All three parts are optional (for(;;) is a valid infinite loop), but normally all three are present.
for (int i = 0, j = 10; i < j; i++, j--) {
System.out.println(i + " " + j);
}A while loop checks the condition first — if it's false right at the start, the loop won't run even once. Use this when you don't know how many times to repeat, just that it should continue while a condition stays true.
A do-while runs the body first, then checks the condition — so it guarantees the loop runs at least once, even if the condition is false from the start. do-while is commonly used in menu-driven programs (show the menu first, then check if the user chose to exit).
int choice;
do {
choice = getMenuChoice(); // pehli baar zaroor chalega
} while (choice != 0);The for-each loop (for (Type item : collection)) is the simplest, most readable way to traverse arrays and collections — no index to manage, no risk of "off-by-one" errors.
Limitation: you can't know the current index (if you need it), and you can't modify (add/remove) the collection while traversing it — doing so throws a ConcurrentModificationException. And it only goes forward, no reversing or skipping.
int[] scores = {90, 85, 77};
for (int s : scores) {
System.out.println(s);
}One loop inside another — very common for printing patterns, traversing 2D arrays, or comparing every pair of items. The outer loop controls rows, the inner loop columns — the outer runs once, the inner runs *fully*.
Understanding time complexity matters here: if the outer loop runs N times and the inner also N times, that's N×N = N² total operations — this can get slow for large N.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
// *
// * *
// * * *for(;;) or while(true) create an infinite loop — normally created by accident (a bug, when you forget to update the condition), but they're intentionally used in server programs, game loops (which should run continuously), or "poll until event" situations, along with an internal break condition.
If an accidental infinite loop happens, the program "hangs" (never finishes) — you have to hit the "stop" button in your IDE. So whenever you write a loop, ask yourself: "will this condition ever become false?"