A developer writes a nested loop to search a 2D grid for a target value. When the value is found in the inner loop, they use `break` expecting the program to exit both loops. Instead, only the inner loop ends and the outer loop keeps running. Why?
ABreak exits all enclosing loops back to the top level of the program
BBreak only exits the innermost enclosing loop; the outer loop continues executing normally
CBreak causes a runtime error when used inside nested loops
DBreak in the inner loop sets a flag that causes the outer loop to exit on its next condition check
Break always exits only the innermost enclosing loop — it has no awareness of outer loops. After the inner loop exits, execution continues at the statement immediately after the inner loop, which in this case is still inside the outer loop. To break out of multiple levels of nesting, the developer needs to set a flag variable checked by the outer loop, use a labeled break (in languages that support it), or restructure the code as a function with a return statement.
Question 2 Multiple Choice
What is the functional difference between `break` and `continue`?
ABreak works only in while loops; continue works only in for loops
BBreak terminates the entire program; continue terminates only the current function call
CBreak exits the loop entirely, jumping to the statement after the loop; continue skips the rest of the current iteration and jumps back to the loop's condition check
DBreak can only be used once per loop body; continue can be used multiple times
Break and continue both alter normal loop execution, but in opposite directions: break exits the loop completely, while continue only exits the current iteration and lets the loop keep going. A useful mental model: break says 'I'm done with this loop entirely'; continue says 'I'm done with this particular iteration, but the loop should keep running.' Both affect only the innermost enclosing loop.
Question 3 True / False
In a nested loop, a `break` statement in the inner loop will exit most enclosing loops, returning control to the code after the outermost loop.
TTrue
FFalse
Answer: False
Break only exits the innermost enclosing loop — it does not propagate outward. After break exits the inner loop, the outer loop continues its next iteration as normal. This is one of the most common sources of bugs when using break in nested loops. Languages like Java and labeled loops in some other languages offer labeled break to exit outer loops, but the default behavior in virtually all languages is innermost-loop-only.
Question 4 True / False
A `continue` statement causes the rest of the current iteration to be skipped, and the loop then proceeds to evaluate its condition (or execute its update step in a for loop) before potentially starting the next iteration.
TTrue
FFalse
Answer: True
This is exactly how continue works. In a while loop, continue jumps back to the condition check. In a for loop, continue jumps to the update expression (e.g., i++) before re-checking the condition. This distinction matters: if the logic that would terminate the loop is in the update step of a for loop, continue correctly executes it — unlike a while loop where a continue that skips an update step could create an infinite loop.
Question 5 Short Answer
A developer is processing a list of user-submitted numbers and wants to skip any value that is negative, then compute a running total of the valid values. Explain how `continue` would be used here and why it might be preferable to wrapping the processing in an `if (value >= 0)` block.
Think about your answer, then reveal below.
Model answer: Using continue: at the top of the loop, check `if (value < 0) { continue; }`. This immediately skips to the next iteration for negative values. The total-update code then runs unconditionally for all non-negative values. The alternative — wrapping everything in `if (value >= 0) { ... }` — produces the same behavior but indents all processing code one level deeper. Continue creates a 'guard clause' pattern that handles the invalid case first and keeps the main logic at the top level of the loop, making it easier to read when the main logic is substantial. Both are valid; the preference for continue grows as the processing code grows longer.
The continue-as-guard pattern is a common idiom for filtering or input validation inside loops. It keeps the 'normal path' code at the shallowest indentation level, which improves readability when the loop body is complex. The key tradeoff: a single `if (value < 0) continue;` at the top clearly signals 'we're filtering negatives,' while a deeply nested if block buries the main logic.