Course outline · 0% complete

0/27 lessons0%

Course overview →

The while loop

lesson 5-2 · ~10 min · 16/27

Loop while a condition holds

A for loop is for "do this N times", but often you cannot know N in advance. Keep asking until the user types a valid answer, keep downloading until the file ends, keep the game running until someone wins. That "repeat for as long as some condition holds" shape is what the second kind of loop exists for.

A while loop keeps going as long as a condition is True:

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

Before every iteration, Python checks the condition count > 0, a comparison from lesson 4-1. If it is True the body runs, and if it is False Python skips past the loop and continues below it.

The line count = count - 1 is the update pattern from lesson 3-2, and it is what makes the loop finish. Without it, count stays 3, the condition stays True, and the program loops forever. That is called an infinite loop, and it is the classic while-loop bug.

The difference between the two loops comes down to who controls the count. A for loop over a range decides in advance how many iterations there will be, and a while loop leaves that to the body.

A countdown, traced

The condition is checked before each iteration, and the body moves count toward failing it.

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

Output

3
2
1
Liftoff!

Tracing the value of count at each condition check gives 3, 2, 1, and then 0. When it reaches 0 the condition 0 > 0 is False, so the loop ends and the final print runs.

The last print is unindented, which is why it runs once after the loop rather than on every iteration. Indenting it would print Liftoff! three times, and that indentation is the only thing distinguishing the two cases.

A loop that never ends

n = 5
while n > 0:
    print(n)

The body never changes n, so the condition stays True forever. This is an infinite loop, and it prints 5 without stopping.

n starts at 5 and is never updated, so n > 0 is True on every check. Adding n = n - 1 inside the body moves n toward 0 and lets the loop end.

The habit that prevents this is to write the update line at the same time as the while line, before filling in the rest of the body. Every while loop needs something that moves it toward False, and adding that something last is how it gets forgotten.

Doubling until a limit

n starts at 1 and doubles each iteration while it stays at or below 16.

n = 1
while n <= 16:
    print(n)
    n = n * 2

Output

1
2
4
8
16

Reading the pieces

  • The condition is while n <= 16:, using <= so that 16 itself is printed. With < the output would stop at 8.
  • Inside the body, print comes first and the update second. Swapping them would print 2, 4, 8, 16, 32 and skip the starting value entirely.
  • After printing 16, n becomes 32, the condition fails, and the loop stops. Notice that n ends up holding a value the loop never printed, which is normal and worth remembering when you use a loop variable afterwards.
  • This is a case where for would be awkward. The number of iterations depends on doubling rather than counting, so the condition-driven loop is the natural fit.