The Plain Picture#
"Stir the pot until it boils." "Put a sticker on each of the 30 notebooks." "Walk up the stairs one step at a time from step 1 to step 20." Repetition with a stopping rule.
The Real Names#
while— repeat while a condition is true (you don't know in advance how many times).for— repeat with a counter (you know the range).for-each (enhanced
for) — repeat once for each item in an array or collection.do-while— likewhile, but the body always runs at least once.break— leave the loop now.continue— skip the rest of this round, go to the next.
Under the Hood#
for — the loop you'll write most#
for (int i = 0; i < 5; i++) {
System.out.println("Question " + (i + 1));
}
// │ │ │
// start keep after each round
// (once) going (then check again)
// whileFlow: run int i = 0 once → check i < 5 → run the body → run i++ → check again → … → stop when the check is false.
Loop shapes you'll use in DSA:
for (int i = 0; i < n; i++) // 0, 1, ..., n-1 — every index of an array of length n
for (int i = n - 1; i >= 0; i--) // n-1, ..., 0 — backwards
for (int i = 0; i < n; i += 2) // 0, 2, 4, ... — every other index
for (int i = 1; i * i <= n; i++) // 1 .. √n — divisors, primes
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) // every PAIR (i, j) with i < j — the brute-force pair loopwhile — when you don't know how many rounds#
int n = 1234,
digits = 0;
while (n > 0) {
n /= 10; // chop off the last digit (integer division, Ch 3)
digits++;
}
// digits == 4Always make sure something in the body moves toward the stopping condition, or the loop runs forever (an infinite loop; stop it with Ctrl+C — The Linux Command Line Ch 1).
for-each#
int[] marks = { 72, 95, 60, 88 };
for (int m : marks) {
// "for each int m in marks"
System.out.println(m);
}Cleaner when you only need the values, not the index. You can't change the array's contents through m, and you don't know the position.
break, continue and labelled break#
for (int m : marks) {
if (m < 0) continue; // skip invalid entries
if (m == 100) break; // stop at the first perfect score
...
}break only exits the innermost loop. To exit two nested loops at once, label the outer one:
outer: for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (grid[r][c] == target) {
System.out.println("found at " + r + "," + c);
break outer; // leaves BOTH loops
}
}
}Gotchas
Off-by-one errors —
i <= ninstead ofi < nreads one past the end of an array (Ch 8). Before running, check the first and last values of your loop variable by hand. This is the most common bug in coding interviews.Changing the loop variable inside a
forbody makes loops very hard to reason about. Use awhileloop if you need that control.
Check yourself
4 questionsHow many times does for (int i = 3; i < 10; i += 3) run, and with which values?
Show answerHide answer
3 times: 3, 6, 9.
When would you choose while over for?
Show answerHide answer
When the number of rounds isn't known in advance, only a stopping condition (e.g. "until n reaches 0").
break vs continue?
Show answerHide answer
break leaves the loop entirely; continue skips to the next round.
Write a loop that visits every pair (i, j) with 0 ≤ i < j < n.
Show answerHide answer
for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { ... }