c# · 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 find what you need halfway through a loop. break stops the loop straight away, and C# carries on with the first line after it.
continue is gentler. It skips the rest of this turn only, then the loop moves on to the next one, like skipping a song you do not like.
Both usually sit inside an if, so they only happen when a condition is true.
using System;
for (int song = 1; song <= 5; song++)
{
if (song == 4)
{
break;
}
Console.WriteLine($"Playing song {song}");
}
Console.WriteLine("Party over!"); break leaves the loop, while continue stays in it and starts the next turn.
guess first
Skip a song. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
for (int song = 1; song <= 4; song++)
{
if (song == 2)
{
continue;
}
Console.WriteLine($"Playing song {song}");
}
your turn
Your turn. Ride the lift.
Choose which floor to get off at, from 1 to 10, and type it in the blank. The lift stops there with break.
output
press run to see what happens
fix it
Cookie 3 is broken, so skip it. But packing stops! Fix it.
output
press run to see what happens
- Run it. Cookies 4 and 5 never get boxed. Which word stops the whole loop?
breakleaves the loop for good. You only want to skip one turn.- Change
breaktocontinueon line 7, then press run.
nailed it
you can stop a loop early now. :D
breakstops the whole loop straight away.continueskips one turn and keeps the loop going.- Both usually sit inside an
if.
new words in your collection
- break
- continue
This lesson isn’t saved. A free account remembers every lesson you finish.