01 / 20

Break & Continue

Master Java's two most powerful loop control statements to write cleaner, more efficient code.

Java Programming

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.

break
Immediately exits the entire loop. Execution jumps to the first statement after the loop.
continue
Skips the rest of the current iteration and jumps back to the loop condition check.

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.

1
Early termination: Stop searching once you find what you need instead of looping through remaining items.
2
Selective processing: Skip invalid or unwanted data without deeply nesting your logic.
3
Performance: Avoid unnecessary computation by exiting or skipping when the result is already known.

Where They Work

Both break and continue operate inside loop constructs. They affect only the innermost enclosing loop by default.

for loop
Both break and continue work inside for loops, including enhanced for-each loops.
while loop
Both keywords work identically: break exits, continue re-evaluates the condition.
do-while loop
continue jumps to the condition at the bottom. break exits immediately.

The break Statement

LOOP Iteration Condition? true break; false Loop body After loop

The continue Statement

LOOP Iteration Condition? true continue; false Loop body

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

1
Identify the goal. Are you searching for something (use break) or filtering items (use continue)?
2
Write the condition. Place an if statement with the exact condition that triggers the control change.
3
Place the keyword. Put break; or continue; immediately inside the if-block.
4
Test boundary cases. What if the condition triggers on the first iteration? The last? Never?
5
Verify the scope. In nested loops, break/continue only affects the innermost loop.

Example: Finding a Target Value

int[] nums = {3, 7, 2, 9, 5}; int target = 9; for (int i = 0; i < nums.length; i++) { if (nums[i] == target) { System.out.println("Found at index " + i); break; // stop searching } }

Example: Skipping Odd Numbers

for (int i = 1; i <= 6; i++) { if (i % 2 != 0) { continue; // skip odd numbers } System.out.println(i); }

Nested Loops: Inner vs Outer

for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) { break; // only exits inner loop } System.out.println(i + "," + j); } }

continue in a while Loop

int i = 0; while (i < 5) { i++; if (i == 3) { continue; // skips println for i==3 } System.out.println(i); }

Labeled break: Exiting Outer Loops

// Label on the outer loop outer: for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == 1 && j == 1) { break outer; // exits BOTH loops } System.out.println(i + "," + j); } }

Common Mistakes

✕ Infinite loop with continue
Placing the loop variable increment after a continue in a while loop. The increment never runs, so the condition never changes.
✕ Wrong loop targeted
Using break in nested loops and expecting the outer loop to stop. Without a label, only the innermost loop is affected.
✕ continue in switch
Writing continue inside a switch statement that is not inside a loop. This causes a compile-time error.

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?