Skip to the workspace
Pebellslearn by building
Loops and Iterations  ›  Lesson

break and continue

break / continue 12 min

javascript / break and continue

Explore each scene at your own pace.

01 · break and continue

break and continue

break and continue

break exits its targeted loop. continue skips the rest of the current iteration and advances to the next iteration.

Skipping an item and ending the whole search are different decisions.

See the mechanism

Walk through the modelbreak and continueAuthored snapshots of the example; use Run below to test the code.
value0
outputnone
continue bypasses the remaining body for zero.
1 of 3

Follow one example

javascript

Zero is skipped with continue. The next value, 4, prints. At 6, break ends the loop, so the later 2 is never visited. In a for loop, continue still reaches the update step. In a while loop, ensure the skipped path does not bypass the only update and accidentally prevent termination.

Predict, change and check

Predict: If break becomes continue in the second condition, will 2 be printed?

Test your explanation: Make that replacement and compare the output. Explain which item was skipped and whether the loop ended.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Know which execution region changes

break exits the selected loop or switch. continue skips the remainder of the current loop iteration and proceeds to the loop's next-step behavior. Neither automatically returns from the enclosing function. A guard inside a loop can skip one invalid record without abandoning all later records.

Nested loops require a clear target for control flow. Labels exist, but a small extracted function can sometimes make the stopping rule easier to review. Test an invalid first item, an invalid middle item and a valid later item to distinguish 'skip one' from 'stop everything'.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.