Questions: Loop Control: Break and Continue

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

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
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
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
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
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.