Unravel โบ Java concepts โบ Language basics
Loops, labels and do-while
break leaves the nearest loop, continue skips to the next round, and a label (outer:) lets either one target an outer loop.
At a glance
Walking through the rooms of a building floor by floor: continue is skipping a room, break is leaving the floor, and break outer is leaving the whole building.
- 1for when the number of rounds is known, while when it depends on a condition, do-while when the body must run first (menus, retry until valid input), and for-each for collections and arrays
- 2break ends the innermost loop (or a switch case)continue jumps to the next iteration (running the for update step first).
- 3outer: for (...) { for (...) { if (found) break outer; } } stops both loopsLabels are rare, and often a sign that the code should be a separate method with return.
- 4Infinite loops written on purpose: while (true) { ..if (done) break; } or for (;;); a thread loop should also check the interrupt flag.
- Off-by-one errors live here: i < n versus i <= n, starting at 0 or 1, and updating the loop variable inside the body
- A for loop variable declared in the header is scoped to the loop; a variable used after the loop must be declared before it
- A for loop compiles to a conditional jump back to the start; a labelled break is a plain goto to the end of the labelled statement in bytecode
public class Grid { static int[] find(int[][] grid, int target) { int[] pos = {-1, -1}; outer: for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[r].length; c++) { if (grid[r][c] < 0) continue; if (grid[r][c] == target) { pos[0] = r; pos[1] = c; break outer; } } } return pos; } static int readValid(java.util.Scanner in) { int n; do { n = in.nextInt(); } while (n < 0); return n; }}Read the full explanation (about 2 min)
break leaves the nearest loop, continue skips to the next round, and a label (outer:) lets either one target an outer loop. do { ... } while (cond) always runs its body at least once.
A mental model
Walking through the rooms of a building floor by floor: continue is skipping a room, break is leaving the floor, and break outer is leaving the whole building.
How it works
for when the number of rounds is known, while when it depends on a condition, do-while when the body must run first (menus, retry until valid input), and for-each for collections and arrays.
break ends the innermost loop (or a switch case). continue jumps to the next iteration (running the for update step first).
outer: for (...) { for (...) { if (found) break outer; } } stops both loops. Labels are rare, and often a sign that the code should be a separate method with return.
Infinite loops written on purpose: while (true) { ... if (done) break; } or for (;;); a thread loop should also check the interrupt flag.
Going deeper
Off-by-one errors live here: i < n versus i <= n, starting at 0 or 1, and updating the loop variable inside the body. Reading a loop out loud with a tiny example ("n = 3: i = 0, 1, 2") catches most of them.
A for loop variable declared in the header is scoped to the loop; a variable used after the loop must be declared before it.
Extracting the loop body into a method with early return is usually more readable than labelled breaks.
Under the hood
A for loop compiles to a conditional jump back to the start; a labelled break is a plain goto to the end of the labelled statement in bytecode. The JIT unrolls and optimises simple counting loops heavily.
Common mistakes
- Off-by-one in the loop bound.
- Modifying the collection while looping.
- An infinite loop from a missing update.
- Overusing labels.
How Unravel recognises it
When you paste code, Unravel looks for these signs of loops, labels and do-while. Spotting them yourself is a good habit when you read unfamiliar code:
- Language constructs:
labeldo
Part of a learning path
- Modern Java syntax ยท next: var and type inference
Related concepts
Frequently asked questions
What is Loops, labels and do-while?
break leaves the nearest loop, continue skips to the next round, and a label (outer:) lets either one target an outer loop. do { ... } while (cond) always runs its body at least once.
What are the most common mistakes with Loops, labels and do-while?
Off-by-one in the loop bound. Modifying the collection while looping. An infinite loop from a missing update. Overusing labels.
How can I see Loops, labels and do-while in real code?
Paste your own Java into Unravel and it will point out where loops, labels and do-while appears, explain how it works at the detail level you choose, and draw the flow of the code around it.