← ACARA Digital Technologies Years 7–8

Choosing a for loop or a while loop (Python)

Use for when you know how many times to repeat, like 10 times. Use while when repeating depends on a condition that can change, like until the user types quit.

Understanding Choosing a for loop or a while loop (Python) in ACARA Digital Technologies Years 7–8

What it does: A fixed count of 5 is a perfect fit for for with range(5). lives starts at 0, so lives > 0 is False straight away and the loop body never runs. You don't know how many tries the user will need, so the loop depends on a condition. Use while.

Common mistakes: A for loop goes through a sequence such as range(10). It cannot take a condition. Either use 'while count < 10:' or write 'for count in range(10):'. The loop should keep going until the user types quit, so the condition must be text != 'quit'. With == the condition is False at the start, so the loop never runs.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 522–560) and word search (Modules 522–580).

Take this worksheet further — free with Cloodit

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

Learning
Duo
You want to print a message exactly 10 times. Which loop fits best?
while True:for i in range(10):
Matches
while i in range(10): print(i)
↓
A while loop needs a condition that can change, not range(). For a fixed count of 10, use a for loop: for i in range(10):
Applying
Meanings
Term
for vs while
Meaning
Use for when you know how many times to repeat, like 10 times. Use while when repeating depends on a condition that can change, like until the user types quit.
Practice
for lap in range(1, 4):
Testing
Gaps
i in range(10):
Check Answer
Spots
1count = 0
2for count < 10:

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

The 'Reef Shark' badge — Reef Ruler, 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. You want to print a message exactly 10 times. Which loop fits best?
A. while True:
B. for i in range(10): ✓
You know the count is 10, so a for loop with range(10) is the best choice.
2. A program keeps asking for a password until the user types the right one. Which loop fits best?
A. for
B. while ✓
You don't know how many tries the user will need, so the loop depends on a condition. Use while.
3. True or false: a for loop is a good choice when you know how many times to repeat.
A. True ✓
B. False
It is true. for loops with range() are made for a fixed count, like 10 times.
4. Which task is best for a while loop?
A. Keep asking for a word until the user types quit ✓
B. Print the numbers 1 to 5
Typing quit could happen at any time, so the loop depends on a changing condition. Counting 1 to 5 is a fixed count, so use for.
5. Which loop is best for printing the 5 times table from 1 to 12?
A. for n in range(1, 13): ✓
B. while answer != 'quit':
The table has a fixed 12 steps, so use a for loop. The while condition here has nothing to do with the task.

Match the Pairs

for i in range(10): print(i) → A for loop: use it when you know the loop must run a fixed number of times, such as 10
while answer != "quit": answer = input("> ") → A while loop: use it when repeating depends on a condition that can change, like until the user types quit
You know in advance that you want to repeat exactly 5 times, so choose a for loop → for turn in range(5):
A while loop may run zero times if its condition is False the very first time it is checked → lives = 0 while lives > 0:
while i in range(10): print(i) → A while loop needs a condition that can change, not range(). For a fixed count of 10, use a for loop: for i in range(10):
for answer != "quit": answer = input("> ") → A for loop cannot repeat until a condition changes. Use while answer != "quit": because we do not know how many times the user will type.

Key Term

for vs while
Use for when you know how many times to repeat, like 10 times. Use while when repeating depends on a condition that can change, like until the user types quit.

Fill in the Blanks

1. for i in range(10): print("Hello")
Hint: Type the loop keyword you use when you know the exact number of repeats.
2. command = input("Command: ") while command != "quit": command = input("Command: ")
Hint: Type the loop keyword that keeps going while a condition stays true.
3. for i in range(0): print("Hi") print("Done")
Hint: Type the number that makes the loop body run zero times.
4. word = "" while word != "quit": word = input("Type: ")
Hint: Type the comparison operator that keeps the loop running while the word is not the stop word.
5. # Show Go! 10 times for i in range(10): print("Go!")
Hint: Type the number of repeats that the comment says is needed.

Spot the Error

Question 1
A count = 0
B for count < 10:
C print(count)
D count += 1
Line B — A for loop goes through a sequence such as range(10). It cannot take a condition. Either use 'while count < 10:' or write 'for count in range(10):'.
Question 2
A text = ''
B while text == 'quit':
C text = input('Type: ')
D print('Bye')
Line B — The loop should keep going until the user types quit, so the condition must be text != 'quit'. With == the condition is False at the start, so the loop never runs.
Question 3
A answer = ''
B while answer != 'quit':
C input('> ')
D print('Goodbye')
Line C — The typed value is thrown away, so answer never changes and the while condition stays True forever. The line should be 'answer = input('> ')'.
Question 4
A n = 1
B while n <= 10:
C print(n)
D n -= 1
Line D — n goes down instead of up, so n <= 10 is always True and the loop never ends. A while loop must change its condition variable: n += 1.
Question 5
A print('Lap counter')
B laps = 10
C for lap in laps:
D print(lap)
Line C — laps is a single number, so a for loop cannot go through it and Python raises a TypeError. For a fixed count, use range(laps).

Student with a class code? Join your class →