▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
break and continue
Stop a loop early or skip a step.
🎬 Video version coming soon — read it now
Sometimes you need to leave a loop early, or skip one round. Two keywords do that.
- break stops the loop completely
- continue skips the rest of this round and moves to the next
break: stop when you've found it
Once the answer is found there's no need to check the other numbers.
The while True pattern
A very common way to write a menu or a "keep going until quit" program:
while True would loop forever on its own, so the break inside is the only way out.
continue: skip this one
Add only the positive numbers:
Use them sparingly
break and continue are useful, but loops full of them become hard to follow. If you have three or four, there is usually a clearer way to write the condition.
In one line
break leaves the loop now, continue skips to the next round, and while True with a break is the classic menu loop.
Share with a friend