continue in a while loop: keep the update first
If i++ were at the bottom of the body, continue would skip it and the loop would never end.
break leaves the innermost loop or switch at once; continue skips the rest of the current iteration and moves to the next. A label lets either one target an outer loop, which is the clean way to stop nested loops.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Print the numbers from 1 to 20, skipping multiples of 4, and stop completely when you reach a number greater than 15.
Find the first pair of numbers in {3, 8, 11, 4, 7} that add up to 15, using nested loops and a labelled break, and print the pair.
Using continue, print only the words in {"apple", "", "kiwi", " ", "plum"} that are not blank.
Explain it without notes
What's the difference between break and continue?
Why does a break inside a switch inside a loop not stop the loop, and how do you stop the loop?
How does continue behave differently in a for loop and a while loop?
What alternatives to a labelled break exist for leaving nested loops?
Expected output
1 3 5 7 9