What is the difference between break and continue? How do you leave two nested loops at once?
Show the explanation
breakends the innermost loop (orswitch) immediately.continueskips the rest of the current iteration and goes on to the next one.
To leave nested loops, put a label on the outer loop and use break label:
outer:
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] < 0) continue; // skip negative cells
if (grid[i][j] == target) {
System.out.println("found at " + i + "," + j);
break outer; // leaves both loops
}
}
}continue outer also exists and jumps to the next iteration of the outer loop. Many teams prefer moving the nested loops into a method and using return instead, which reads more clearly.
Original UniCircle question. Last reviewed 22 Oct 2026.
