Leaving early: break and continue
Stop a loop the moment you have found what you need with break, skip the rest of one iteration with continue, and use while True with break for loops that end in the middle.

In this lesson you will
- Use break to leave a loop early
- Use continue to skip to the next iteration
- Predict the output of a loop containing break or continue
- Write a while True loop that ends with break
Sometimes a loop doesn’t need to go all the way round. If you’re searching a shelf of books for
one title, you stop as soon as you find it. If you’re counting the red cars in a car park, you
glance at a blue one and move straight on to the next. Python has two small words for exactly
these situations: break and continue.
break: stop the loop now
break ends the loop immediately. Python jumps to the first line after the loop, even if
there were more iterations to go.
for n in range(1, 10):
if n == 5:
break
print(n)
print("Loop finished")It prints 1, 2, 3, 4, then Loop finished. When n reaches 5, the if is True, break
runs, and the loop is over. The print(n) for 5 never happens, and 6, 7, 8 and 9 are never even
looked at.
continue: skip to the next one
continue ends only the current iteration. Python skips the rest of the block and goes
straight back to the top of the loop for the next value.
for n in range(1, 10):
if n % 2 == 0:
continue
print(n)This prints only the odd numbers. For even numbers, continue skips the print; for odd numbers
the if is False, so the print runs.
Watch them work
The visualiser shows each iteration as a tile. A tile marked break is where the loop
stopped; tiles after it are never reached. A tile marked continue was visited, but the rest
of its block was skipped. Try the dropdowns to add a break, a continue, or both, and step
through.
This loop runs through one to nine, but two extra checks can change its course.
Notice the order of the checks in the code: the break check comes first, then the continue
check, then the body. Order matters here, just as it did with elif.
Searching with break
break is perfect for searching: look at candidates one by one and stop at the first one that
fits. What’s the first number above 100 that divides exactly by both 7 and 9?
for n in range(101, 1000):
if n % 7 == 0 and n % 9 == 0:
print("Found it:", n)
breakWithout the break, the loop would carry on and print every match up to 1000. Try deleting it
to see.
Filtering with continue
continue is handy for skipping items you don’t care about, so the main work isn’t buried
inside an if. Add up the numbers from 1 to 20, but skip multiples of 3:
total = 0
for n in range(1, 21):
if n % 3 == 0:
continue
total += n
print(total)You could write the same thing without continue, by putting total += n inside
if n % 3 != 0:. Both are correct. continue reads well when there are several reasons to skip
an item, each on its own line at the top of the loop.
while True … break
Sometimes the natural place to decide whether to stop is in the middle of the loop, not at
the top. A common pattern is a loop that would run forever, while True:, with a break inside
it:
balance = 100
months = 0
while True:
balance = balance * 1.05
months += 1
if balance >= 150:
break
print(f"After {months} months the balance is {balance:.2f}")while True on its own would be an infinite loop, because True is always true. The break is
the way out. When you use this pattern, make sure the break can actually be reached.
break only leaves one loop
If loops are nested, break and continue affect only the innermost loop they’re in:
for row in range(1, 4):
for col in range(1, 4):
if col > row:
break
print(row, col)The break ends the inner col loop each time, but the outer row loop carries on.
Your turn
Exercise · Python
First match
Solved ✓Find the first number from 1 upwards that leaves a remainder of 1 when divided by 2, 3, 4, 5 and 6, but divides exactly by 7. (This is a very old puzzle about counting eggs in a basket.)
Use a for loop over range(1, 1000) and break as soon as you find it. Store the number in
answer, and count how many numbers you checked in checked.
answer = None
checked = 0
for n in range(1, 1000):
checked += 1
# if n fits every rule, store it in answer and stop the loop
print(answer, "found after checking", checked, "numbers")
Check yourself
Self-check · 4 questions