typescript · level 1 · unit 5 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
stops a loop early. skips a turn.
Sometimes you want out of a loop before it finishes. break jumps straight out, like leaving a queue. The program carries on after the loop.
continue is gentler. It skips the rest of this lap and goes on to the next one, like a lift that does not stop at floor 13.
Both of them usually live inside an if, so they only happen at the right moment.
for (let floor = 10; floor <= 16; floor++) {
if (floor === 13) {
continue;
}
console.log("Ding! Floor", floor);
} break and continue end with a semicolon, like every other instruction.
guess first
What happens at biscuit 3? Make a guess.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
for (let biscuit = 1; biscuit <= 5; biscuit++) {
if (biscuit === 3) {
break;
}
console.log("Biscuit", biscuit);
}
your turn
Your turn. Skip a number.
Pick a number from 1 to 5 to skip and type it in the blank. Then press run and watch it vanish from the list.
fix it
Box 3 is broken, but pack the rest. Fix it!
- Only boxes 1 and 2 get packed. What does
breakdo to the whole loop? breakstops the loop for good. You only want to skip box 3.- Change break to continue on line 3, then press run.
nailed it
you can stop a loop whenever you like. :D
breakjumps out of a loop straight away.continueskips the rest of this lap only.- Put them inside an
ifso they happen at the right time.
new words in your collection
- break
- continue
This lesson isn’t saved. A free account remembers every lesson you finish.