Fundamentals · Lesson 10 of 94

while loops, break, and continue

Watch this lesson

View full playlist ↗

Read the lesson

What you'll learn

Concept

A while loop is an if that keeps coming back. Python checks the condition; if it is truthy it runs the block, then returns to the top and checks again. The loop ends the first time the condition is falsy. That is the whole mechanism, and everything about loops follows from it.

while loops, break, and continue: start: set up a variable → run the block → change something → carry on after the loop
while loops, break, and continue · Figure 1 · Open for full size

Every correct loop has three parts. Something is set up before the loop, like count = 0. The condition tests it, like count < 5. And inside the block, something changes it, like count += 1. If the block never changes anything the condition depends on, the condition never becomes false and the loop runs forever. That is the infinite loop, and every programmer writes one eventually; when it happens, press Ctrl+C to stop the program.

break exits the loop immediately, no matter what the condition says. It is how you write "loop until something happens" when the something is discovered in the middle of the block, like reading input until the person types quit. continue skips the rest of the current round and jumps back to the test, which is a clean way to ignore values you do not want to process.

while loops, break, and continue: in the block → leave the loop → back to the test → finish the block
while loops, break, and continue · Figure 2 · Open for full size

A while True: loop with a break inside is a common and accepted pattern. It says: keep going until I tell you to stop. Use it when the natural exit condition only becomes known part-way through the block. Use a plain while condition: when the test fits on the first line, because then the reader sees the exit condition immediately.

Code

The basic shape: set up, test, change. Watch count climb to 5 and the loop stop the moment the test fails.

# countdown.py
count = 0

while count < 5:
    print("count is", count)
    count += 1

print("done, count is", count)
Output
count is 0
count is 1
count is 2
count is 3
count is 4
done, count is 5

A loop that stops when it finds what it is looking for. continue skips every number that is not a multiple of 7, and break ends the loop at the first one that is, so nothing after 21 is ever checked.

# first_multiple.py
n = 20

while True:
    n += 1
    if n % 7 != 0:
        continue
    print("first multiple of 7 above 20:", n)
    break
Output
first multiple of 7 above 20: 21

continue skipping unwanted values. Odd numbers are skipped before the print, so only evens appear, and the total counts only what was printed.

# evens.py
n = 0
total = 0

while n < 10:
    n += 1
    if n % 2 == 1:
        continue
    total += n
    print(n, end=" ")

print()
print("total of evens:", total)
Output
2 4 6 8 10 
total of evens: 30

Try it yourself

Create guess.py with a secret number stored in a variable and a list of guesses to try, like [50, 25, 37, 42]. Loop through the guesses with a while and an index variable (indexing a list works like indexing a string; lists get their own lesson soon), printing "too high" or "too low" for each, and break with a success message when a guess matches. If the list runs out, print how many guesses were used.

Common mistakes

What's next

You can repeat until a condition changes. Next comes the loop you will write far more often: for, which walks through a collection or a range of numbers without you managing a counter at all.

Course overview