← Back to Loops Part 2
πŸ“–

break & continue β€” How They Work

1 of 6

Loops Part 2 β€” break and continue

You already know for and while loops. Now it's time to learn two powerful keywords that give you finer control over how your loops run.


break β€” Exit the Loop Early

break immediately stops the loop, even if the condition is still True or there are more items to iterate over.

for i in range(10): if i == 5: print("Found 5! Stopping.") break print(i)

Output:

0
1
2
3
4
Found 5! Stopping.

The loop was supposed to go to 9, but break ended it at 5.


break with while True

A common pattern is to use while True (an infinite loop) and break to exit when a condition is met:

count = 0 while True: count += 1 if count > 3: break print(count) print("Done!")

Output:

1
2
3
Done!

This is useful when you don't know the exact stopping condition upfront, or when the exit check needs to happen in the middle of the loop body.


continue β€” Skip to the Next Iteration

continue skips the rest of the current iteration and jumps back to the top of the loop.

for i in range(6): if i % 2 == 0: continue print(i)

Output:

1
3
5

Even numbers hit continue, so their print is skipped. The loop still runs all 6 times β€” it just doesn't execute the code after continue for those iterations.


Using Both Together

You can combine break and continue in the same loop:

numbers = [4, -1, 7, 0, 3, -5, 8] total = 0 for num in numbers: if num < 0: continue # skip negatives if num == 0: break # stop at zero total += num print(total) # 11 (4 + 7)

Quick Reference

KeywordWhat it doesWhen to use
breakExits the loop immediatelySearching for something, stopping early
continueSkips to the next iterationFiltering out certain items
while True + breakLoop until you decide to stopUnknown number of iterations

Key Rules

  1. break and continue only affect the innermost loop they're in
  2. Code after break or continue (in that iteration) does not run
  3. break exits the loop entirely β€” continue just skips one iteration
  4. Don't overuse them β€” sometimes a better loop condition is cleaner
πŸ’»

Try it yourself

Code: break & continue β€” How They Work

Loading Python runtime…
Python …
Loading...
Output
Click "Run" to execute your code...
ℹ️ About this Python environment

βœ… Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect

βœ… Functions, classes, recursion, print()

❌ No file system, subprocess, OS access, or network requests

❌ No pip install (all supported modules are pre-loaded)

⏱️ 5 second execution time limit