← Python

While Loops

A while loop repeats its code block as long as its condition remains True

What it does: 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. while, like if and for, requires a colon at the end of the line. The loop prints 1, 2, 3 then stops when i becomes 4. x decrements from 10 to 9 to 8. At 8 the condition fails, then prints 8. Repeats indented code as long as the condition is True.

Common mistakes: Code inside the while loop must be indented. Missing colon at the end of the while line.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a journey and assign it to your class.

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
When does a while loop stop?
When the condition becomes FalseAfter 10 times
Matches
❌ while x < 10 x += 1
Error: Missing colon after while
Applying
Meanings
Term
while
Meaning
Repeats indented code as long as the condition is True.
Practice
i = 0
Testing
Gaps
i < 5:
Check Answer
Spots
2while i < 5:
3print(i)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Snow Leopard' badge — Silent Debugger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. When does a while loop stop?
A. When the condition becomes False ✓
B. After 10 times
while keeps looping as long as its condition is True
2. i=0\nwhile i<3:\n print(i)\n i=i+1\nWhat prints?
A. 1, 2, 3
B. 0, 1, 2 ✓
i starts at 0 — it prints 0, 1, 2 then when i=3 the condition i<3 fails
3. What happens with while True: and no break?
A. The loop runs forever ✓
B. It runs once
True never becomes False — the loop continues indefinitely
4. What does the condition in a while loop check?
A. How many times to loop
B. Whether to keep looping ✓
The condition is checked before each iteration — if False, stop
5. count=5\nwhile count>0:\n print(count)\n count=count-1\nWhat prints first?
A. 5 ✓
B. 0
The condition is checked first — 5>0 is True, so 5 prints before decrementing

Match the Pairs

while x < 10 x += 1 Error: Missing colon after while
i = 0 while i < 5: print(i) Error: Infinite loop — never increments i
i = 0 while i < 3: i += 1 Valid: Loop runs 3 times
while True: if done: break Ok: Infinite loop with break
i = 1 while i <= 3: print(i) i += 1 Output: 1 2 3
x = 10 while x > 8: x -= 1 print(x) Output: 8

Key Term

while
Repeats indented code as long as the condition is True.~~Condition

Fill in the Blanks

1. i = 0 while 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
A i = 0
B while i < 5:
C print(i)
D i = i + 1
Line C — Code inside the while loop must be indented
Question 2
A i = 0
B while i < 5
C print(i)
D i = i + 1
Line B — Missing colon at the end of the while line
Question 3
A i = 0
B while i < 5:
C print(i)
D # missing i = i + 1
Line C — Missing i = i + 1: the counter never increases, causing an infinite loop
Question 4
A i = 0
B While i < 5:
C print(i)
D i = i + 1
Line B — while must be lowercase: While is not valid Python
Question 5
A i = 0
B while i < 5:
C print(i)
D i = i + 1
E print('Done')
Line E — 'Done' is indented inside the loop it prints 5 times instead of once
For Teachers

Create a free class, add this module to a journey, and track who's stuck.

Create a free teacher account →

No credit card. No setup fee. Make a class and invite students in under three minutes.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?