php · 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 before the loop is done. break stops the loop on the spot, and PHP carries on with the first line after it.
continue is gentler. It skips the rest of this go and jumps straight to the next one, so the loop keeps running.
Both usually sit inside an if, so they only happen when you want. In this lift, continue skips unlucky floor 13 but stops at all the others.
<?php
for ($floor = 12; $floor <= 15; $floor++) {
if ($floor === 13) {
continue;
}
echo "Ding! Floor $floor\n";
} break and continue only work inside a loop, so PHP refuses to run them anywhere else.
guess first
The basket is full at apple 3. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<?php
for ($apple = 1; $apple <= 5; $apple++) {
if ($apple === 3) {
break;
}
echo "Picked apple $apple\n";
}
your turn
Your turn. Stop running when you are tired.
The loop could run 100 laps, but you will get tired long before that. Type the lap you stop after, from 1 to 10, then run it.
fix it
Skip burnt cookie 3, but box all the others. Can you fix it?
- Run it. Cookies 4 and 5 never get boxed. What happens to the loop at cookie 3?
breakstops the whole loop. You only want to skip this one turn.- Change
breaktocontinueon line 4, then press run.
nailed it
you can stop and skip inside loops. :D
breakstops the whole loop straight away.continueskips the rest of this go only.- Put them inside an
ifso they happen when you want.
new words in your collection
- break
- continue
This lesson isn’t saved. A free account remembers every lesson you finish.