← All lessons

Lesson 19

While Loops

A while loop repeats as long as its condition is true. Use it when you do not know in advance how many repeats are needed.

Something inside the loop must move toward ending it, such as increasing a counter. Otherwise the loop never stops.

break exits a loop early, and continue skips to the next repeat. Prefer a clear condition over many breaks.

count = 3
while count > 0:
    print(count)
    count -= 1

Check your understanding

What happens if a while condition never becomes false?

CHALLENGE

Start n at 1 and double it while it is below 50. Print n after the loop finishes.

Practice →
Next: Splitting & Joining Text →