While Loops, online

Six activities your students do on their own devices. See who's finished, and their scores.

While Loops in one minute

What students need to know

i starts at 0 and increments each loop. When i reaches 3, condition is False. while True runs forever — use break to exit when a condition is met.

True never becomes False — the loop continues indefinitely. The condition is checked first — 5>0 is True, so 5 prints before decrementing.

Mistakes students make

  • Code inside the while loop must be indented.
  • Missing colon at the end of the while line.
  • Missing i = i + 1: the counter never increases, causing an infinite loop.
  • while must be lowercase: While is not valid Python.

Discover other journeys with this module

What students will learn in this module

Every question in Module 19

Quick questions

  1. When does a while loop stop?AAfter 10 timesBWhen the condition becomes False
  2. i=0\nwhile i<3:\n print(i)\n i=i+1\nWhat prints?A0, 1, 2B1, 2, 3
  3. What happens with while True: and no break?AThe loop runs foreverBIt runs once
  4. What does the condition in a while loop check?AWhether to keep loopingBHow many times to loop
  5. count=5\nwhile count>0:\n print(count)\n count=count-1\nWhat prints first?A0B5

Key terms

Students type the meaning of:

  • while
  • Condition
  • Infinite loop
  • Counter variable

Match the pairs

while x < 10
    x += 1
Match it to what it does
i = 0
while i < 5:
    print(i)
Match it to what it does
i = 0
while i < 3:
    i += 1
Match it to what it does
while True:
    if done:
        break
Match it to what it does
i = 1
while i <= 3:
    print(i)
    i += 1
Match it to what it does
x = 10
while x > 8:
    x -= 1
print(x)
Match it to what it does

Fill in the blanks

  1. i = 0
    _____ i < 5:
        print(i)
        i = i + 1
  2. i = 0
    while i < 5_
        print(i)
        i = i + 1
  3. i = 0
    while i _ 5:
        print(i)
        i = i + 1
  4. i = 0
    while i < 5:
        print(i)
        i = i _ 1
  5. count = 3
    while count _ 0:
        print(count)
        count = count - 1

Spot the error

Question 1

Ai = 0Bwhile i < 5:Cprint(i)Di = i + 1

Question 2

Ai = 0Bwhile i < 5C    print(i)D    i = i + 1

Question 3

Ai = 0Bwhile i < 5:C    print(i)D    # missing i = i + 1

Question 4

Ai = 0BWhile i < 5:C    print(i)D    i = i + 1

Question 5

Ai = 0Bwhile i < 5:C    print(i)D    i = i + 1E    print('Done')