What do the break and continue statements do inside a loop in Java?
answer
- break = leave the loop
- continue = skip to next iteration
- for: continue -> update then condition
- while: continue -> condition directly (increment trap)
- innermost loop only
basics
~10 sbreak 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 sBoth 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
Can state that break leaves the loop and continue skips one iteration, and read simple examples correctly.
Explains the for-vs-while difference (where continue jumps) and the while increment-trap that causes infinite loops.
Discusses readability trade-offs, prefers guard-clause continue over deep nesting, and knows break also stops switch fall-through.
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