Break & Continue
Master Java's two most powerful loop control statements to write cleaner, more efficient code.
What Are Loop Control Statements?
Loops repeat code, but sometimes you need to change the flow mid-loop. Java gives you two keywords to do exactly that: break and continue. They let you exit a loop early or skip specific iterations without restructuring your logic.
Why Do We Need Them?
Without break and continue, you'd need complex boolean flags and nested if-statements to control loop behavior. These keywords make your intent explicit and readable.
Where They Work
Both break and continue operate inside loop constructs. They affect only the innermost enclosing loop by default.
The break Statement
The continue Statement
break vs continue: Key Difference
| break | continue | |
|---|---|---|
| Effect | Exits the entire loop | Skips to next iteration |
| Remaining iterations | Abandoned | Still execute |
| Where execution goes | After the loop | Back to loop header |
| Works in switch? | Yes | No |
| Think of it as | "I'm done looping" | "Skip this one" |
How to Apply Them
Example: Finding a Target Value
Example: Skipping Odd Numbers
Nested Loops: Inner vs Outer
continue in a while Loop
Labeled break: Exiting Outer Loops
Common Mistakes
Complete Reference Table
| break | continue | |
|---|---|---|
| Syntax | break; | continue; |
| Labeled syntax | break label; | continue label; |
| for loop | Exits loop entirely | Runs update, re-checks |
| while loop | Exits loop entirely | Re-checks condition |
| Nested loops | Exits innermost (or labeled) | Skips in innermost (or labeled) |
| Common use case | Search found, stop early | Filter/skip invalid data |
Question 1 of 5
What is the output of this code?
Question 2 of 5
What is the output of this code?
Question 3 of 5
In a nested loop, an unlabeled break statement will:
Question 4 of 5
What happens when this code runs?
Question 5 of 5
When continue is used inside a for loop, what executes immediately after the continue statement?