PY-101 ยท Loops
break, continue and loop else
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
break ends a loop immediately, continue skips the rest of the current round and goes to the next one. A loop can also have an else block, which runs only if the loop finished without hitting break. Together they cover "stop when found" and "skip this one".
- Stop a loop with break
- Skip a round with continue
- Use for ... else for searches
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
orders = [1200, -5, 800, 52000, 300]for amount in orders:if amount < 0:print("Skipping bad value", amount)continueif amount > 50000:print("Big order found:", amount)breakelse:print("No big order")
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Skipping bad value -5
Big order found: 52000 ๐ฏ Find the first number divisible by 7 in nums and print "Found 21". If none is divisible by 7, print "None". Use break and a loop else.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
if n % 7 == 0: print("Found", n); break, then else: print("None")
- The problemYou are scanning 1,000 orders for the first one over โน50,000. Once you find it, there is no reason to read the other 999.
- The Python idea
for item in items:break ends a loop immediately, continue skips the rest of the current round and goes to the next one. - In real lifeLog scanners skip blank lines with continue and stop at the first critical error with break.
Start with a question
You are scanning 1,000 orders for the first one over โน50,000. Once you find it, there is no reason to read the other 999.
Searching, skipping bad data and stopping early are everyday jobs. These three keywords make them short and clear.
Explain it like I'm 10
break is "found it, stop looking". continue is "not this one, next please".
The technical explanation
break jumps straight out of the nearest loop. Code after the loop runs next.
continue jumps back to the top for the next item, skipping the rest of the block for this one.
for ... else: runs the else block only when the loop ended normally. It is perfect for "not found" messages.
Syntax
for item in items:
if skip_this:
continue
if found_it:
break
else:
... # no break happened Real-world scenario
Log scanners skip blank lines with continue and stop at the first critical error with break.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Nested loops โQuestions people ask
Does break exit all loops in Python?
No, only the innermost loop it is in. To leave nested loops, use a flag, put the loops in a function and return, or restructure the search.
What is for else in Python used for?
For searches: the else block runs when the loop finished without break, which means nothing was found.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner Does break exit all loops in Python?
No, only the innermost loop it is in. To leave nested loops, use a flag, put the loops in a function and return, or restructure the search.
beginner What is for else in Python used for?
For searches: the else block runs when the loop finished without break, which means nothing was found.
intermediate What does break do?
Ends the loop. It leaves the nearest loop immediately.
intermediate What does continue do?
Skips to the next round. The rest of this round is skipped.
Quick revision
Remember
- Stop a loop with break
- Skip a round with continue
- Use for ... else for searches
- The else on a loop is easy to misread: it means "no break happened", not "the loop was empty".
Syntax
for item in items:
if skip_this:
continue
if found_it:
break
else:
... # no break happened Official documentation
- break and continue (docs.python.org)