Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Loops and Control Flow  ›  Lesson

break and continue

break / continue 10 minbreak exits a loop immediately and for good; continue skips only the rest of the current lap and moves on to the next one — both act on the nearest enclosing loop.

break and continue

In short: both change a loop's normal flow, but differently. break exits the loop immediately — no more laps run, ever. continue skips only the rest of the current lap and jumps straight to the next one — the loop keeps going. Reach for break when you've found what you needed and there's no point checking further; reach for continue when this particular item doesn't apply but the rest still might.

A race track: lap 2 has a red exit ramp labelled break that leaves the track for good; lap 3 has a green chicane labelled continue that skips the rest of that lap only, and the track keeps going to lap 4

Skipping songs marked to skip

continue lets a loop pass over items that don't apply, without stopping:

javascript

When song is "skip", continue ends that lap right there — the console.log below it never runs for that item — and the loop moves on to "Paper Moon" as normal. Every other song still plays.

Stopping a queue at a marker

break is for "we're done here, for good":

javascript

The loop never even looks at "Paper Moon" — once break runs, control jumps straight past the entire loop to "after loop". Nothing after the break in that lap, and no later lap, ever executes.

Choosing between them

The test: does the rest of the loop still matter after this item, or not? Skipping an explicit-content song while playing the rest of the playlist is continue. Stopping playback entirely because the user hit pause is break. Mixing them up is a real bug — a break where you meant continue silently drops every item after the trigger, not just the one you meant to skip.

Quick practice

quick drill

for (const n of [1, 2, 3, 4]) { if (n === 2) continue; console.log(n); } — what prints?

quick drill

for (const n of [1, 2, 3, 4]) { if (n === 2) break; console.log(n); } — what prints?

quick drill

A loop needs to skip blank entries but keep checking the rest of the list. Which keyword?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainSkip one, or stop entirely?

A playlist loop needs to: (1) skip any song marked "explicit" and keep going, and (2) stop entirely once it hits a song marked "STOP". Explain which keyword does which job and why swapping them would break the logic.