← Years 7–8 Creating Digital Solutions

Nested if blocks in pseudocode

An if block written inside another if block. The inner condition is only checked when the outer condition is true. Indent the inner block and give each if its own end if.

Understanding Nested if blocks in pseudocode in Years 7–8 Creating Digital Solutions

What it does: Indenting each inner block shows it is inside the block above it, which makes the algorithm easier to read and trace. The print only runs when raining is true and haveUmbrella is true, which matches: if raining = true AND haveUmbrella = true then. Every if needs its own end if. The inner end if closes the inner block and the outer end if closes the outer block. 40 is not >= 50, so the outer condition is false. The computer never looks at the inner condition. Nesting lets you run extra steps after the outer check passes and add a separate else for the inner check. AND combines the checks into one.

Common mistakes: The inner if is missing the keyword then. It should be: if has_ticket = true then. The inner if ends with the wrong word. The keyword is then, not than. It should be: if score >= 90 then.

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
In a nested if, when is the inner condition checked?
Only when the outer condition is trueEvery time, whether the outer condition is true or not
Matches
if age >= 12 then if hasTicket = true then print "Enter" end if
↓
The outer if has no end if. Every if, including the outer one, needs its own end if.
Applying
Meanings
Term
nested if
Meaning
An if block written inside another if block. The inner condition is only checked when the outer condition is true. Indent the inner block and give each if its own end if.
Practice
if hasTicket = true then
Testing
Gaps
tall = true then print "Ride" end if
Check Answer
Spots
1if age >= 12 then
2 if has_ticket = true

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

The 'Hedgehog' badge — First Steps, 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. In a nested if, when is the inner condition checked?
A. Only when the outer condition is true ✓
B. Every time, whether the outer condition is true or not
The inner if sits inside the outer if's body, so it only runs if the outer condition is true.
2. Which pseudocode shows the inner block indented correctly?
A. if age >= 12 then if hasTicket = true then print "Enter" end if end if
B. if age >= 12 then if hasTicket = true then print "Enter" end if end if ✓
Indenting the inner block shows which if it belongs to and makes the algorithm easier to read and trace.
3. A nested if has 2 if blocks, one inside the other. How many end if lines does it need?
A. 2, one for each if ✓
B. 1, because they are joined together
Every if needs its own end if. The inner end if closes the inner block and the outer end if closes the outer block.
4. Which is the correct order for closing a nested if?
A. Close the outer if first, then the inner if
B. Close the inner if first, then the outer if ✓
The inner block is inside the outer block, so it must be closed first.
5. score = 40. Outer if: score >= 50. Inner if: score = 100. What happens to the inner if?
A. It is skipped, because the outer condition is false ✓
B. It is checked, and found to be false
40 is not >= 50, so the outer condition is false. The computer never looks at the inner condition.

Match the Pairs

if age >= 12 then if hasTicket = true then → A nested if: the inner if is written inside the outer if, so hasTicket is only checked when age >= 12 is true
end if end if → Each if block gets its own end if. The inner one closes first, then the outer one
The inner block is indented so you can see which if it belongs to → if score > 50 then if score > 90 then print "Top mark" end if end if
A nested if can do the same job as AND, because both conditions must be true for the action to run → if raining = true then if haveUmbrella = true then print "Take umbrella" end if end if
if age >= 12 then if hasTicket = true then print "Enter" end if → The outer if has no end if. Every if, including the outer one, needs its own end if.
if temp > 30 then if humid = true then print "Stay inside" end if end if → No indenting, so it is hard to see that the second if is nested inside the first.

Key Term

nested if
An if block written inside another if block. The inner condition is only checked when the outer condition is true. Indent the inner block and give each if its own end if.

Fill in the Blanks

1. if age >= 12 then if tall = true then print "Ride" end if end if
Hint: Type the keyword that starts the inner block inside the outer if.
2. if age >= 12 then if tall = true then print "Ride" end if end if
Hint: Type the two-word keyword that closes the outer if block.
3. if score >= 50 then if score >= 80 then print "Merit" end if end if
Hint: Type the comparison operator that means 'greater than or equal to'.
4. if age >= 12 and tall = true then print "Ride" end if
Hint: Type the Boolean operator that lets one if replace two nested ifs.
5. if raining = true then if windy = true then print "Coat" end if end if
Hint: Type the keyword that comes after the inner condition.

Spot the Error

Question 1
A if age >= 12 then
B if has_ticket = true
C print "Enter"
D end if
E end if
Line B — The inner if is missing the keyword then. It should be: if has_ticket = true then
Question 2
A if score >= 50 then
B if score >= 90 than
C print "Excellent"
D end if
E end if
Line B — The inner if ends with the wrong word. The keyword is then, not than. It should be: if score >= 90 then
Question 3
A if temp > 30 then
B print "Hot day"
C if humidity => 70 then
D print "Muggy"
E end if
F end if
Line C — The comparison operator => is not valid. 'Greater than' is > (or >= for 'greater than or equal to'). It should be: if humidity > 70 then
Question 4
A if logged_in = true then
B if is_admin = true then
C say "Admin panel"
D end if
E end if
Line C — say is not a pseudocode keyword. Output is written with print. It should be: print "Admin panel"
Question 5
A if temp > 25 then
B print "Warm"
C if temp > 35 then
D print "Very hot
E end if
F end if
Line D — The text being printed is missing its closing quote. It should be: print "Very hot"

Student with a class code? Join your class →