ReviseAlgo Logo

Control Flow

break and continue

Control execution flow using break and continue keywords, exploring early loop termination and iteration skips.

Interview: Tests nested loop branch jumps, execution pathways, and unreachable statement compiler flags.

Last Updated: June 13, 2026 10 min read

In Java, break and continue are jump statements used to alter the execution flow of loops. While break terminates the loop immediately, transferring control to the statement following the loop, continue terminates only the current iteration, skipping the remaining loop body statements and jumping to the next iteration check.

Core Idea

Break exits loop blocks completely. Continue skips the remaining body code and jumps directly to the loop's update and condition check.

Why It Matters

Allows loops to exit early (e.g., when a target item is found), saving CPU cycles and preventing redundant execution loops.

Interview Lens

Expect questions targeting compile-time unreachable statements, nested loop exit jumps, and for vs while update execution behavior.

Unreachable Statement Errors

The compiler tracks execution paths. If you write code immediately after an unconditional break or continue statement, the compiler flags it with an "unreachable statement" error, preventing compilation:

while (true) {
    break;
    System.out.println("Hello"); // Compile-time error: unreachable statement
}

Execution flow differences: For vs While

When continue is executed:

  • In a for loop, control jumps to the update expression (e.g., i++), then to the condition check.
  • In a while / do-while loop, control jumps directly to the condition check, skipping any updates. If update operations were placed at the bottom of the loop body, they will be skipped, which can lead to infinite loop traps.

Common Pitfalls

  • Unconditional Infinite Loops in While: Placing continue in a while loop before updating the iterator variable, causing the loop to check the same state indefinitely.
  • Confusing Break with Return: Using break thinking it exits the method entirely, when it only exits the innermost loop.
  • Unreachable Statements: Placing statement logic immediately below unconditional jump statements.

Best Practices

  • Use break to exit loops early when search conditions are met.
  • Use continue to skip invalid elements at the start of loop bodies, keeping the primary logic clean and unindented.
  • Limit the use of nested loops that rely on multiple jumps to keep code easy to read and trace.

Interview-Relevant Information

Q1: What happens to the update statement in a for-loop when continue is executed?
Answer: The update statement (e.g., i++) is executed immediately after continue runs. It is not skipped. Only the remaining statements in the loop body are bypassed.

Q2: Can break be used outside of loops or switches?
Answer: Standard break can only be used inside loops or switch statements. However, labeled break can be used to exit any labeled block of code, though this is rarely used in practice.

Quick Checklist

Can you trace loop execution flows, resolve unreachable compiler statements, manage updates during skips, and terminate search iterations early? If yes, you understand jump statements.

Use Cases

Skipping malformed records in data streams while continuing to process remaining elements.

Terminating search algorithms early as soon as the target item matches.

Common Mistakes

Creating infinite loop traps in while statements by placing continue before index updates.

Writing statements immediately after unconditional jumps, causing compilation failures.