← Years 7–8 Creating Digital Solutions

Decisions inside Python loops

An if indented inside a loop's block is checked again on every repetition, so each pass can make a different decision.

Understanding Decisions inside Python loops in Years 7–8 Creating Digital Solutions

What it does: The if is indented inside the for loop, so every n is tested and count only goes up when n is even. The if sits inside the while block, so each new guess gets its own check on every repetition. The variable being tested can change on each pass, so the condition can be True one time and False the next. The if is checked for n = 1, 2, 3 and 4, but it is only True when n is 3. So the message prints once. The if is checked on all 3 passes. n is only 1, 2 or 3, so n > 5 is False every time and print never runs.

Common mistakes: The else line needs a colon at the end. It should be 'else:' so that the indented block below it belongs to the else. range is a function, so it needs round brackets, not square ones. It should be 'for i in range(5):'.

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 50–125) and word search (Modules 3–125).

Take this worksheet further — free with Cloodit

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

Learning
Duo
for n in range(5): if n > 2: print(n) How many times is the condition n > 2 checked?
5 times, once on every repetitionOnly once, before the loop starts
Matches
for n in range(1, 6): print(n) if n == 3: print("Three!")
↓
The if is not indented, so it is outside the loop. It runs only once after the loop ends, and only checks the last value of n.
Applying
Meanings
Term
if inside a loop
Meaning
An if indented inside a loop's block is checked again on every repetition, so each pass can make a different decision.
Practice
for num in range(1, 11):
Testing
Gaps
n > 3:
Check Answer
Spots
3 print(n, "is even")
4 else

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

The 'Firefly' badge — Night Coder, 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. for n in range(5): if n > 2: print(n) How many times is the condition n > 2 checked?
A. 5 times, once on every repetition ✓
B. Only once, before the loop starts
The if is indented inside the loop, so it is checked again on every pass. Here that is 5 times, even though print only runs for some of them.
2. Which code checks every number from 1 to 10 and prints only the even ones?
A. if n % 2 == 0: for n in range(1, 11): print(n)
B. for n in range(1, 11): if n % 2 == 0: print(n) ✓
The if must be indented inside the for loop so each number gets its own decision. In the wrong version the if is outside the loop, and n has no value yet.
3. True or false: an if inside a loop can make a different decision on each repetition.
A. False
B. True ✓
The variable being tested can change on each pass, so the condition can be True one time and False the next.
4. Which line of this code makes the if part of the loop's block? for score in scores: if score >= 50: print("Pass")
A. Indenting the if line under the for line ✓
B. Writing the if line with no indent
Indentation shows which lines belong to the loop. An indented if is repeated on every pass. An if with no indent would run only after the loop is finished.
5. What does this loop print? for n in range(1, 5): if n == 3: print("Three!")
A. Three! (printed once) ✓
B. Three! (printed 4 times)
The if is checked for n = 1, 2, 3 and 4, but it is only True when n is 3. So the message prints once.

Match the Pairs

for n in range(1, 6): if n > 3: print(n) → The if is checked on every pass of the for loop, so only the numbers bigger than 3 are printed.
while lives > 0: if hit: lives = lives - 1 → The if is checked every time the while loop repeats, and lives only goes down on passes where hit is True.
Count only the even numbers by testing each number in a for loop → for n in range(1, 11): if n % 2 == 0: count = count + 1
Keep asking in a while loop and make a decision each time the user answers → while guess != 7: guess = int(input()) if guess < 7: print("Too low")
for n in range(1, 6): print(n) if n == 3: print("Three!") → The if is not indented, so it is outside the loop. It runs only once after the loop ends, and only checks the last value of n.
for score in scores: if score > 50: print("Pass") → The if is not indented under the for line, so Python does not see it as part of the loop block, which causes an error.

Key Term

if inside a loop
An if indented inside a loop's block is checked again on every repetition, so each pass can make a different decision.

Fill in the Blanks

1. for n in range(1, 6): if n > 3: print(n)
Hint: Type the keyword that makes a decision on each pass of the loop
2. for score in [4, 9, 7]: if score > 5: print("Pass")
Hint: Type the comparison operator meaning greater than
3. for n in range(1, 11): if n % 2 == 0: print(n)
Hint: Type the punctuation that ends an if line before its indented block
4. n = 1 while n <= 5: if n == 3: print("Three!") n = n + 1
Hint: Type the loop keyword that repeats while a condition is true
5. for age in [8, 12, 14]: if age >= 12: print("Teen")
Hint: Type the spaces that indent the line inside the if block

Spot the Error

Question 1
A for n in range(1, 6):
B if n % 2 == 0:
C print(n, "is even")
D else
E print(n, "is odd")
Line D — The else line needs a colon at the end. It should be 'else:' so that the indented block below it belongs to the else.
Question 2
A for i in range[5]:
B if i == 3:
C print("Three!")
Line A — range is a function, so it needs round brackets, not square ones. It should be 'for i in range(5):'.
Question 3
A count = 0
B while count > 5:
C if count == 2:
D print("Two!")
E count = count + 1
Line B — count starts at 0, so 'count > 5' is False straight away and the loop (and the if inside it) never runs. It should be 'while count < 5:'.
Question 4
A total = 0
B for n in range(1, 11):
C if n > 5:
D total == total + n
E print(total)
Line D — A double == compares values and does not store anything. Use a single = to update the total: 'total = total + n'.
Question 5
A for score in [40, 75, 90]:
B if score => 50:
C print("Pass")
D else:
E print("Try again")
Line B — '=>' is not a Python comparison operator. Greater than or equal to is written '>=', so it should be 'if score >= 50:'.

Student with a class code? Join your class →