skip to content

break, continue & Labels

break exits the loop, continue skips to the next iteration, and labeled forms let you target an outer loop. Labeled break is the sanctioned alternative to a flag variable when leaving nested loops.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What do the break and continue statements do inside a loop in Java?

level: juniorimportance: must knowfreq 75%

answer

  1. break = leave the loop
  2. continue = skip to next iteration
  3. for: continue -> update then condition
  4. while: continue -> condition directly (increment trap)
  5. innermost loop only

basics

~10 s

break stops the loop entirely and jumps to the code right after it. continue skips the rest of the current iteration and goes straight to the next one (re-checking the loop condition).

solid answer

~40 s

Both control the flow of a loop. break terminates the nearest enclosing loop (or switch) immediately and continues execution after that loop. continue ends only the current iteration: in a for loop it jumps to the update expression then re-tests the condition; in a while/do-while it jumps straight back to the condition test. Neither affects code outside the loop other than where execution resumes. A common use is break to stop searching once you've found a match, and continue to skip invalid or irrelevant items while still processing the rest. Without a label, both act only on the single innermost loop that contains them.

go deeper

for a junior

Can state that break leaves the loop and continue skips one iteration, and read simple examples correctly.

for a middle

Explains the for-vs-while difference (where continue jumps) and the while increment-trap that causes infinite loops.

for a senior

Discusses readability trade-offs, prefers guard-clause continue over deep nesting, and knows break also stops switch fall-through.

for a principal

Frames break/continue as structured-programming jump statements, weighs them against early return / extracted methods, and sets team conventions for loop control clarity.

## What a loop is A *loop* repeats a block of code. Java has `for`, `while`, `do-while`, and the enhanced for (`for-each`). Each loop runs *iterations* until some condition stops it. Sometimes you want to leave a loop early, or skip part of one pass — that's what `break` and `continue` are for. They are *jump statements*: they move execution to a different place instead of running the next line in sequence. ## break `break;` immediately ends the **nearest enclosing loop** and resumes at the first statement *after* that loop. Nothing left in the loop body runs, and the loop condition is not checked again. ```java for (int i = 0; i < 10; i++) { if (i == 3) break; // leaves the loop System.out.println(i); // prints 0, 1, 2 } // execution continues here ``` `break` is also used in `switch` statements to stop fall-through; that is a separate use of the same keyword. ## continue `continue;` ends only the **current iteration** and moves on to the next one. The rest of the loop body below `continue` is skipped, but the loop itself keeps going. Where it jumps depends on the loop type: - In a **for** loop it jumps to the *update* part (e.g. `i++`), then re-tests the condition. - In a **while** / **do-while** loop it jumps straight to the *condition* test. ```java for (int i = 0; i < 5; i++) { if (i % 2 == 0) continue; // skip even numbers System.out.println(i); // prints 1, 3 } ``` ## A subtle for-vs-while trap Because `continue` in a `while` loop skips straight to the condition, any increment written *after* the `continue` is never reached — an easy way to create an infinite loop: ```java int i = 0; while (i < 5) { if (i == 2) continue; // BUG: i never advances, loops forever i++; } ``` A `for` loop avoids this because the update expression always runs after `continue`. ## Scope: innermost loop only Unlabeled `break`/`continue` affect only the single loop directly containing them. With nested loops, a plain `break` exits just the inner loop — the outer loop keeps running. To affect an outer loop you need a *label* (covered separately). ## Summary - `break` = exit the whole loop now. - `continue` = skip to the next iteration now. - Both act on the innermost loop unless labeled.

  • In a for loop, does continue skip the i++ update expression?
    No. In a for loop continue jumps to the update part (i++) and then re-evaluates the condition, so the update still runs.
  • Can break be used outside a loop?
    Yes, in a switch statement to prevent fall-through. Using break with no enclosing loop or switch is a compile error.

saying these in an interview costs you the question

  • Saying continue exits the loop (it only skips one iteration)
  • Saying break ends the program or method (it ends the loop)
  • Forgetting that continue in a while loop can cause an infinite loop if the increment sits below it
  • Thinking unlabeled break exits all nested loops

context

open as a page

How do labeled break and labeled continue work in Java, and when would you use them?

level: middleimportance: should knowfreq 50%

basics

~20 s

You 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.

open as a page

Precisely where does control transfer for break and continue in for, while, and do-while loops, including the for loop's update expression?

level: middleimportance: should knowfreq 45%

basics

~20 s

break sends control to right after the loop in all loop types. continue goes to the loop's condition check, but in a for loop it runs the update part (like i++) first. So a for loop's update always runs on continue; a while/do-while jumps straight to the condition.

open as a page

With nested loops in Java, which loop does an unlabeled break or continue affect, and why does that matter?

level: middleimportance: should knowfreq 55%

basics

~10 s

An unlabeled break or continue only affects the innermost loop that directly contains it. The outer loop keeps running. So a break inside an inner loop just ends that inner loop, not both.

open as a page

When should you prefer break/continue versus an early return or extracted method for controlling loop flow?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Use break/continue when the loop's work belongs in the current method and you just need to stop or skip. Prefer extracting the loop into its own method and using return when the loops are deeply nested or the logic is complex, because return exits everything cleanly and reads better.

open as a page