Velamuvi AIAcademy

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
In this lesson you will learn
  • 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.

Try it yourselfPython
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.

Try it yourselfPython
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

breakcontinue
What it doesEnds the whole loopEnds only the current pass
Where Python goes nextThe line after the loopThe next item in the loop
Typical use"I found it, stop searching""Ignore this one, carry on"

Compare the two on the same data.

Try it yourselfPython
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.

Try it yourselfPython
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.

Try it yourselfPython
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

Practice exercise

Loop through numbers and print the first one that is divisible by 7. Make sure the loop stops there.

Your solutionPython
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
Knowledge check

What does continue do inside a loop?

Summary

  • break ends a loop immediately.
  • continue skips to the next pass.
  • while True: with a break repeats until a condition inside the loop is met.
  • In nested loops, break and continue affect 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.