What you'll learn
- Repeat a block while a condition stays true
- Recognize the three parts every loop needs: start, test, and change
- Leave a loop early with
breakand skip one round withcontinue - Spot an infinite loop before you write it
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.

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.

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)
count is 0
count is 1
count is 2
count is 3
count is 4
done, count is 5A 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
first multiple of 7 above 20: 21continue 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)
2 4 6 8 10
total of evens: 30Try 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
- Forgetting the change.
while count < 5:with nocount += 1inside runs forever. If a program hangs, pressCtrl+Cand look for the missing update. - Putting the change after a
continue. Ifn += 1comes aftercontinue, the skip jumps over it and the loop never advances. - Testing the wrong boundary.
while count <= 5runs six times, from 0 to 5. Decide whether you want<or<=before you type it.
What's next
- A
whileloop is a repeatingif: test, run the block, test again, until the condition is falsy. - Every loop needs a start, a test, and a change; a missing change is an infinite loop, stopped with
Ctrl+C. breakleaves the loop at once,continueskips to the next test, andwhile Truewithbreakis a normal pattern.
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.