Lesson 15 of 30 · Loops
Break and Continue
Control your Python loops with break and continue. Stop a loop early, skip unwanted items and use the while True pattern safely.
- 6 min read
- Beginner
- Reviewed
- How to stop a loop early with break
- How to skip one pass of a loop with continue
- The while True pattern and when to use it
Leaving a loop early with break
Sometimes you find what you are looking for before the loop is finished. There is no reason to keep going. break ends the loop immediately.
names = ["Ada", "Grace", "Alan", "Linus", "Guido"]
for name in names:
print("Checking", name)
if name == "Alan":
print("Found Alan!")
break
print("Search finished.")Linus and Guido are never checked. As soon as Python reaches break, it jumps to the first line after the loop.
Skipping one item with continue
continue does not end the loop. It ends only the current pass and jumps straight to the next item.
temperatures = [21, -999, 24, 19, -999, 23]
for temperature in temperatures:
if temperature == -999:
continue
print("Reading:", temperature)Here -999 marks a broken sensor reading. When the loop meets one, continue skips the print and moves on.
break and continue side by side
break | continue | |
|---|---|---|
| What it does | Ends the whole loop | Ends only the current pass |
| Where Python goes next | The line after the loop | The next item in the loop |
| Typical use | "I found it, stop searching" | "Ignore this one, carry on" |
Compare the two on the same data.
print("With break:")
for number in range(1, 6):
if number == 3:
break
print(number)
print("With continue:")
for number in range(1, 6):
if number == 3:
continue
print(number)The while True pattern
while True: creates a loop that would run for ever, because True is always true. Paired with break, it is a clean way to repeat until the user chooses to stop.
while True:
message = input("You: ")
if message == "quit":
print("Goodbye!")
break
print("You said:", message)This is easier to read than the version from the last lesson, because input() is written only once.
Both keywords work in both kinds of loop
break and continue work the same way in for and while loops. This example uses continue to skip invalid input and break to stop.
total = 0
while True:
number = int(input("Number (0 to finish): "))
if number == 0:
break
if number < 0:
print("Negative numbers are ignored.")
continue
total += number
print("Total:", total)The last input line, 99, is never read, because the loop ended at 0.
Common mistakes
Loop through numbers and print the first one that is divisible by 7. Make sure the loop stops there.
numbers = [3, 10, 14, 21, 5] # Find the FIRST number that can be divided by 7. # Print it and stop the loop. Output must be exactly: 14
What does continue do inside a loop?
Summary
breakends a loop immediately.continueskips to the next pass.while True:with abreakrepeats until a condition inside the loop is met.- In nested loops,
breakandcontinueaffect only the innermost loop.
You have finished the module on loops. Next, you will learn to store many values in one variable.
Finished reading? Tick it off to track your progress.