How do labeled break and labeled continue work in Java, and when would you use them?
answer
- label: before the loop
- break label = exit that loop
- continue label = next iteration of that loop
- only way to target an outer loop directly
- no goto in Java; this is the restricted substitute
basics
~20 sYou put a name (a label) followed by a colon before a loop, like outer:. Then break outer; exits that labeled loop completely, and continue outer; jumps to the next iteration of that labeled loop. This lets you control an outer loop from inside an inner one.
solid answer
~50 sA label is an identifier followed by a colon placed immediately before a statement, almost always a loop: e.g. `outer: for (...) {...}`. From inside a nested loop, `break outer;` terminates the entire loop labeled `outer` and resumes after it, while `continue outer;` skips to the next iteration of the labeled loop. This is the only built-in way to break or continue an outer loop directly. The typical use is multi-level loops where you must abort or skip the outer loop based on an inner condition — for example finding an element in a 2D array and bailing out of both loops at once. Labels work with for, while, and do-while. They should be used sparingly: when they make nested control clearer than flags, but excessive labeling can read like a goto, so extracting a method with return is often preferable.
go deeper
Can recognize label syntax (name + colon before a loop) and read what break/continue with a label does.
Writes correct labeled break/continue for nested loops and explains when a label beats a flag.
Judges readability, prefers method extraction with return where it clarifies, and reserves labels for tight algorithms.
Sets codebase conventions: labels permitted but extract-and-return favored; ensures nested control intent is unambiguous and maintainable.
## What a label is In Java a *label* is just a name you attach to a statement so you can refer to it later. The syntax is an identifier, a colon, then the statement: ```java outer: for (int i = 0; i < n; i++) { ... } ``` Here `outer` labels the `for` loop. Labels are typically only useful on loops, because the only statements that reference them are `break label;` and `continue label;`. (Technically you can label any statement and `break` to it, but in practice labels are used with loops.) ## labeled break `break outer;` terminates the **entire loop named `outer`**, even from deep inside a nested loop, and execution resumes at the first statement after that labeled loop. This is the clean solution to the "a plain break only leaves the inner loop" problem. ```java outer: for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (grid[row][col] == target) { break outer; // exits BOTH loops at once } } } // resumes here after both loops ``` ## labeled continue `continue outer;` skips to the **next iteration of the labeled loop**, abandoning the rest of the inner loop *and* the rest of the current outer iteration. ```java outer: for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (shouldSkipRow(i, j)) { continue outer; // jump to the next i, skip rest of this row } } } ``` Without the label, `continue` would only move to the next `j`; with `outer` it moves to the next `i`. ## Rules and constraints - The label must name an **enclosing** loop of the `break`/`continue` statement; you cannot jump to an unrelated or sibling loop. - Labels are scoped to the labeled statement; the same label name can be reused elsewhere. - `continue label;` requires the labeled statement to be a loop (you can't `continue` a labeled block). `break label;` can target any labeled statement, but breaking out of a labeled non-loop block is rare. - Java has **no goto**; labeled break/continue are the structured, restricted substitute. ## When to use — and when not to Use labeled jumps when you genuinely need to control an outer loop from inside an inner one and a label is clearer than the alternatives. They shine in tight algorithms (grid/matrix scans, early-exit searches). Avoid them when: - The nested loops could be extracted into a method, where a plain `return` exits everything and reads more clearly. - A simple guard/flag is obvious. Overuse produces goto-like spaghetti. Many style guides allow labels but recommend method extraction first. ## Summary - `label:` before a loop names it. - `break label;` exits that loop entirely; `continue label;` jumps to its next iteration. - It's the only direct way to break/continue an outer loop. - Use sparingly; prefer extract-and-return when it's clearer.
- Does Java have a goto statement, and how does labeled break relate to it?No, Java reserves goto as a keyword but does not implement it. Labeled break/continue are the structured, restricted alternative: they can only jump out of or continue an enclosing loop, not jump to arbitrary code.
- What is the difference between continue outer; and a plain continue; in nested loops?Plain continue advances the inner loop to its next iteration; continue outer; abandons the rest of the inner loop and advances the labeled outer loop to its next iteration.
saying these in an interview costs you the question
- Putting the label after the loop or on the wrong statement
- Thinking you can break/continue to a sibling or unrelated loop
- Using continue label on a labeled non-loop block (illegal)
- Overusing labels where extract-and-return is clearer
- Confusing labeled break/continue with a goto that can jump anywhere