How do break and continue affect while and do-while loops, including labeled forms?
answer
- break = exit loop; continue = next iteration
- continue re-checks the condition (bottom for do-while)
- Default target = innermost loop
- label: before loop -> break label / continue label
- continue can skip a counter update -> infinite loop
basics
~20 sbreak exits the loop immediately. continue skips the rest of the current iteration and jumps to the next condition check. A label like outer: lets break/continue target an outer loop instead of the innermost one.
solid answer
~40 sInside a while or do-while, break terminates the loop entirely and control jumps to the statement after it. continue stops the current iteration early and re-evaluates the loop condition (in a do-while, continue jumps straight to the condition at the bottom, not back to the top). By default both affect the innermost enclosing loop. To affect an outer loop in nested loops, you put a label before the outer loop (outer:) and write break outer; or continue outer;. A subtle point: with continue in a while loop, if the variable that drives the condition is updated after the continue, you can accidentally create an infinite loop because the update gets skipped. Labeled break is the idiomatic Java way to exit multiple nested loops at once, replacing flag variables or, less cleanly, returns.
code
java · 11 linesouter:
int i = 0;
while (i < 3) {
int j = 0;
while (j < 3) {
if (i == 1 && j == 1) break outer; // leaves both loops
System.out.println(i + "," + j);
j++;
}
i++;
}go deeper
Knows break exits the loop and continue skips to the next iteration.
Uses labeled break/continue for nested loops and avoids the continue+missed-update infinite loop.
Explains continue's bottom-jump in do-while, and weighs labeled break vs extracting a method/return for readability.
Treats deep nesting needing labels as a design smell and guides refactoring toward smaller methods or stream pipelines.
## The two jump statements Within any loop, Java gives you two ways to alter the normal iteration flow: ### `break` `break;` **immediately terminates the enclosing loop**. Execution continues at the first statement *after* the loop. The condition is not re-checked and no further iterations happen. ```java int i = 0; while (true) { if (i == 3) break; // leaves the loop when i hits 3 System.out.println(i); i++; } // control resumes here ``` ### `continue` `continue;` **skips the rest of the current iteration** and proceeds to the loop's next step: - In a `while`, it jumps back up to **re-evaluate the condition**. - In a `do-while`, it jumps **down to the condition** at the bottom (then re-tests). ```java int i = 0; while (i < 5) { i++; if (i % 2 == 0) continue; // skip even numbers System.out.println(i); // prints 1, 3, 5 } ``` ## The continue / infinite-loop trap If the variable controlling the condition is updated *after* the point where you `continue`, the update is skipped and the loop can hang: ```java int i = 0; while (i < 5) { if (i == 2) continue; // BUG: i never advances past 2 -> infinite System.out.println(i); i++; } ``` Fix by ensuring the progress update happens before the `continue`, or restructure the condition. ## Default target: the innermost loop By default, `break` and `continue` act on the **nearest enclosing loop** (or, for `break`, also a `switch`). In nested loops, a plain `break` only escapes the inner one. ## Labeled break / continue A **label** is an identifier followed by a colon placed before a loop. `break label;` exits the labeled loop; `continue label;` continues the labeled loop's next iteration. ```java outer: while (rowsLeft()) { while (colsLeft()) { if (found()) { break outer; // exits BOTH loops at once } if (skipRow()) { continue outer; // jumps to the outer loop's next iteration } } } ``` Labeled `break` is the idiomatic Java solution for leaving multiple nested loops together — cleaner than a boolean flag checked in every loop and more local than extracting the loops into a method just to `return`. Use labels sparingly; deep nesting that needs them is often a hint to extract a method. ## Summary - `break` = leave the loop now. - `continue` = skip to the next condition check. - Both target the innermost loop unless you name a label. - Watch the `continue` + post-update infinite-loop trap in `while`.
- In a do-while loop, where does continue jump to?It jumps to the condition test at the bottom of the loop, then re-evaluates it — not back to the top of the body. If the condition is still true, the body runs again.
- What is a cleaner alternative to a labeled break for exiting nested loops?Extracting the nested loops into their own method and using return to leave them. This avoids labels, keeps each method small, and often reads more clearly; labeled break is the in-place alternative when extraction is overkill.
saying these in an interview costs you the question
- Thinking continue restarts the loop from the top in a do-while — it goes to the bottom condition.
- Believing break/continue can target any loop by default — they hit the innermost unless labeled.
- Forgetting that continue can skip a counter increment and cause an infinite loop.
- Confusing Java labels with goto — Java has no arbitrary goto; labels only work with break/continue.