Python · control flow

While Loops

Repeat a block for as long as a condition stays true.

How a while loop runs

True False repeat count = 0 count < 3 print(count) count += 1 done 0 count
  1. Check the condition.
  2. If it is True, run the indented body, then go back to the check.
  3. If it is False, skip the body and carry on after the loop.
  4. Something in the body must eventually make the condition False, or the loop never ends.

Basic while loop


        # while condition is True
        count = 0
        while count < 5:
            print(count)
            count += 1
        # ans: 0, 1, 2, 3, 4
      

The condition is checked before every pass, including the last one that fails.

While with countdown


        # countdown
        x = 3
        while x > 0:
            print(x)
            x -= 1
        # ans: 3, 2, 1
      
x =

While with break


        # infinite loop with break
        x = 0
        while True:
            x += 1
            if x > 5:
                break
        # ans: x is 6
      

while True never stops by itself: break is the only way out.

While with increment


        # increment by different values
        x = 0
        while x < 10:
            x += 2
        # ans: x is 10 (after the last iteration)
      

Exercises

Predict the result, then press → to trace the loop and check.

Exercise 1


        # what is count after?
        count = 0
        while count < 5:
            count += 1
        # ans: count is 5
      

Exercise 2


        # how many prints?
        x = 3
        while x > 0:
            print(x)
            x -= 1
        # ans: 3 prints (3, 2, 1)
      

Exercise 3


        # what happens?
        x = 0
        while x < 5:
            x += 2
        # ans: x is 6 (after the last iteration)
      

Exercise 4


        # infinite loop?
        x = 0
        while x >= 0:
            x += 1
            if x > 5:
                break
        # ans: x is 6 (breaks when x becomes 6)
      

Exercise 5


        # what is x?
        x = 10
        while x > 0:
            x -= 3
        # ans: x is -2 (10, 7, 4, 1, -2)
      

Exercise 6


        # condition never true?
        x = 0
        while x > 0:
            x += 1
        # ans: the loop never runs, x is 0
      

Exercise 7


        # what prints?
        x = 1
        while x <= 4:
            print(x)
            x *= 2
        # ans: 1, 2, 4
      

Exercise 8


        # tricky: what is x?
        x = 0
        while x < 3:
            x += 1
        else:
            x += 10
        # ans: x is 13 (3 + 10)
      

The else of a loop runs when the loop ends without break. See Loop Else.

Exercise 9


        # what is the final value?
        x = 100
        while x > 1:
            x = x // 2
        # ans: x is 1 (100, 50, 25, 12, 6, 3, 1)
      

Once x reaches 1, 1 > 1 is False, so it never gets halved to 0.

Exercise 10


        # condition check?
        x = 5
        while x:
            x -= 1
        # ans: x is 0 (5, 4, 3, 2, 1, 0)
      

Remember

Check first

The condition is tested before each pass; if it starts False the body never runs.

Change something

The body must move the condition towards False, or the loop runs for ever.

break

Leaves the loop at once, wherever it is in the body.

Truthiness

while x: keeps going while x is non-zero.