← Years 7–8 Creating Digital Solutions

A loop inside an if block

A loop written inside an if branch only runs when that branch's condition is true. If the condition is false, the loop is skipped. An else branch can hold a loop too.

Understanding A loop inside an if block in Years 7–8 Creating Digital Solutions

What it does: When x is 0 or less, the condition is false, so the for loop never starts and nothing prints. 2 > 0 is true, so the loop runs. It counts from 1 to x, which is 2, so it prints 1 and then 2. The loop must sit inside the if block so the condition controls it. In the second option the loop is outside the if, so it always runs. 0 > 0 is false, so the else branch runs. The loop in the else branch repeats 3 times.

Common mistakes: The loop counter is i, not j. The variable j was never created, so the loop inside the if block can't print it. It should be print i. A for loop needs the word to between the start and end values. The loop inside the if block should read for i from 1 to 3.

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 3–47) 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
A loop is written inside an if block. When does the loop run?
Every time the algorithm runsOnly when the if condition is true
Matches
if x > 0 then print "Positive" for i from 1 to x print i
↓
The loop is not indented under then, so it is outside the if block and runs no matter what the condition is
Applying
Meanings
Term
if x > 0 then for i from 1 to x
Meaning
A loop written inside an if branch only runs when that branch's condition is true. If the condition is false, the loop is skipped. An else branch can hold a loop too.
Practice
score <- input
Testing
Gaps
i from 1 to 3
Check Answer
Spots
3 for i from 1 to x
4 print j

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

The 'Platinum Fox' badge — Tundra Legend, 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. A loop is written inside an if block. When does the loop run?
A. Every time the algorithm runs
B. Only when the if condition is true ✓
A loop inside an if branch is only reached when that branch's condition is true. If the condition is false, the loop is skipped.
2. score <- 3 if score > 5 then for i from 1 to 4 print i What is printed?
A. 1 2 3 4
B. Nothing, the condition is false so the loop is skipped ✓
3 > 5 is false, so the algorithm never enters the if block. The for loop inside it never starts.
3. x <- 2 if x > 0 then for i from 1 to x print i What is printed?
A. 1 then 2 ✓
B. Nothing
2 > 0 is true, so the loop runs. It counts from 1 to x, which is 2, so it prints 1 and then 2.
4. Which algorithm only repeats when the answer is 'yes'?
A. if answer = "yes" then for i from 1 to 3 print "Hi" ✓
B. for i from 1 to 3 print "Hi" if answer = "yes" then
The loop must sit inside the if block so the condition controls it. In the second option the loop is outside the if, so it always runs.
5. An else branch can hold a loop too.
A. True ✓
B. False
Loops can go in the true branch, the else branch, or both. The else loop runs only when the condition is false.

Match the Pairs

if score > 10 then for i from 1 to 3 print i → The loop is inside the true branch, so it only runs when score is bigger than 10
else while lives > 0 lives <- lives - 1 → The loop is inside the else branch, so it only runs when the if condition was false
If the condition is false, the loop in the true branch is skipped completely → if x > 0 then for i from 1 to x print i
A loop can sit in the else branch and run only when the condition is false → if age >= 13 then print "Teen" else for i from 1 to 3 print "Kid"
if x > 0 then print "Positive" for i from 1 to x print i → The loop is not indented under then, so it is outside the if block and runs no matter what the condition is
if x > 0 then print "Positive" else for i from 1 to x print i → The loop is in the else branch, but x is only positive in the true branch, so the loop will never run properly

Key Term

if x > 0 then for i from 1 to x
A loop written inside an if branch only runs when that branch's condition is true. If the condition is false, the loop is skipped. An else branch can hold a loop too.

Fill in the Blanks

1. if score > 50 then for i from 1 to 3 print "Pass"
Hint: Type the keyword that starts the loop inside the true branch
2. if x > 0 then for i from 1 to x print i
Hint: Type the variable that sets how many times the loop repeats
3. else for i from 1 to 3 print "Low"
Hint: Type the keyword for the branch that runs when the condition is false
4. if age >= 13 then for i from 1 to 3 print "Hi"
Hint: Type the comparison operator for 'greater than or equal to'
5. if x > 0 then for i from 1 to x print i
Hint: Type the loop counter variable to print each time round

Spot the Error

Question 1
A x <- 3
B if x > 0 then
C for i from 1 to x
D print j
E end for
F end if
Line D — The loop counter is i, not j. The variable j was never created, so the loop inside the if block can't print it. It should be print i.
Question 2
A if score >= 50 then
B print "Pass"
C for i from 1 3
D print "Well done"
E end for
F end if
Line C — A for loop needs the word to between the start and end values. The loop inside the if block should read for i from 1 to 3.
Question 3
A when n > 0 then
B for i from 1 to n
C print i
D end for
E end if
Line A — A selection block starts with the keyword if, not when. The loop only runs if this condition is true, so the line should be if n > 0 then.
Question 4
A if n > 0 then
B for i from 1 to n
C print i
D end if
E end if
Line D — The loop is closed with end for, not end if. The loop must be closed first, then the if block is closed with end if on the last line.
Question 5
A if n > 0 then print n
B else
C for i from 1 to n
D print "Low"
E end for
F end if
Line C — The else branch only runs when n is 0 or less. A loop from 1 to n would be skipped even when the else branch runs. It should be for i from 1 to 3.

Student with a class code? Join your class →