← ACARA Digital Technologies Years 7–8

Closing nested structures in the right order

Each IF and each REPEAT needs its own end line. Close the innermost block first, so END IF comes before END REPEAT when an IF is inside a loop. A missing end line is an error.

Understanding Closing nested structures in the right order in ACARA Digital Technologies Years 7–8

What it does: The IF opens inside the loop and is closed by its own END IF before the loop ends with END REPEAT. Every IF has its own END IF. Each opening line is matched with exactly one end line. The IF opened last, so it is closed first. The loop is closed after it. END IF always closes an IF. END REPEAT is the line that closes the REPEAT. Each IF needs its own END IF, even when both are in the same loop. Every IF needs its own END IF. Without it, the END REPEAT cannot close the loop properly.

Common mistakes: Line 2 opens a second IF, but the outer block is closed by END REPEAT. It should be REPEAT 4 TIMES so that END REPEAT has a REPEAT to match. Line 3 opens a REPEAT, but it is closed by END IF, so the end line does not match its opening line. It should be IF n < 2 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 365–411) and word search (Modules 331–411).

Take this worksheet further — free with Cloodit

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

Learning
Duo
An IF sits inside a REPEAT. Which end line comes first?
END IF, then END REPEATEND REPEAT, then END IF
Matches
REPEAT 4 TIMES IF x > 5 THEN SAY "Big" END REPEAT
↓
The END IF is missing. The IF block is never closed, which is an error. Add END IF before END REPEAT.
Applying
Meanings
Term
END IF / END REPEAT
Meaning
Each IF and each REPEAT needs its own end line. Close the innermost block first, so END IF comes before END REPEAT when an IF is inside a loop. A missing end line is an error.
Practice
SET count ← 0
Testing
Gaps
Check Answer
Spots
1SET total TO 0
2IF total < 10 THEN

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. An IF sits inside a REPEAT. Which end line comes first?
A. END IF, then END REPEAT ✓
B. END REPEAT, then END IF
The innermost block is closed first. The IF is inside the loop, so END IF comes before END REPEAT.
2. Which rule is correct for pseudocode blocks?
A. Each IF needs its own END IF ✓
B. One END IF can close several IF lines
Every IF has its own END IF. Each opening line is matched with exactly one end line.
3. A REPEAT 5 TIMES loop has no END REPEAT. What is this?
A. An error, because the loop is never closed ✓
B. Fine, because the loop ends by itself
A missing end line is an error. Without END REPEAT we can't tell which steps are inside the loop.
4. Which pseudocode closes its blocks correctly? REPEAT 3 TIMES IF score > 5 THEN SAY "Win"
A. Add END REPEAT, then END IF
B. Add END IF, then END REPEAT ✓
The IF opened last, so it is closed first. The loop is closed after it.
5. Which line does this END IF match? REPEAT UNTIL lives = 0 IF hit = true THEN lives ← lives - 1 END IF END REPEAT
A. It matches REPEAT UNTIL lives = 0
B. It matches IF hit = true THEN ✓
END IF always closes an IF. END REPEAT is the line that closes the REPEAT.

Match the Pairs

REPEAT 5 TIMES IF score > 10 THEN SAY "Win" END IF END REPEAT → The IF is inside the loop, so END IF closes first and END REPEAT closes last.
END REPEAT → Closes the REPEAT loop that was opened earlier. It must match a REPEAT line and come after any inner END IF.
Each IF needs its own END IF, even when the IF sits inside a loop. → REPEAT 3 TIMES IF age >= 13 THEN SAY "Teen" END IF END REPEAT
Close the innermost structure first, then the outer one, so the end lines match their opening lines like a stack. → REPEAT UNTIL lives = 0 IF hit = true THEN lives ← lives - 1 END IF END REPEAT
REPEAT 4 TIMES IF x > 5 THEN SAY "Big" END REPEAT → The END IF is missing. The IF block is never closed, which is an error. Add END IF before END REPEAT.
REPEAT 4 TIMES IF x > 5 THEN SAY "Big" END REPEAT END IF → The end lines are in the wrong order. The innermost block (IF) must be closed first, so END IF goes before END REPEAT.

Key Term

END IF / END REPEAT
Each IF and each REPEAT needs its own end line. Close the innermost block first, so END IF comes before END REPEAT when an IF is inside a loop. A missing end line is an error.

Fill in the Blanks

1. SET score TO score + 1 END IF END REPEAT
Hint: Type the end line that closes the inner decision first, before the loop is closed.
2. SET score TO score + 1 END IF END REPEAT
Hint: Type the end line that closes the outer loop, which is closed last.
3. IF lives = 0 THEN SAY "Game over" END IF
Hint: Type the end line that matches the decision opened on the first line.
4. REPEAT 3 TIMES SAY "Hi" END REPEAT
Hint: Type the keyword that opens the structure closed by the last line.
5. SAY "Done" END IF END REPEAT
Hint: Type the word that shows which outer structure this final end line closes.

Spot the Error

Question 1
A SET total TO 0
B IF total < 10 THEN
C IF total < 10 THEN
D ADD 2 TO total
E END IF
F END REPEAT
Line B — Line 2 opens a second IF, but the outer block is closed by END REPEAT. It should be REPEAT 4 TIMES so that END REPEAT has a REPEAT to match.
Question 2
A SET n TO 0
B REPEAT 3 TIMES
C REPEAT 2 TIMES
D SAY "Low"
E END IF
F END REPEAT
Line C — Line 3 opens a REPEAT, but it is closed by END IF, so the end line does not match its opening line. It should be IF n < 2 THEN.
Question 3
A REPEAT 5 TIMES
B IF key = "w" THEN
C SAY "Go"
D END REPEAT
E END REPEAT
Line D — The innermost block must be closed first. The IF is still open on line 4, so this line should be END IF, and END REPEAT comes last on line 5.
Question 4
A REPEAT 2 TIMES
B REPEAT 3 TIMES
C SAY "Welcome"
D END REPEAT
E END IF
Line A — Line 1 opens a REPEAT, but the last line is END IF, so the outer end line has nothing to match. Line 1 should be IF hasTicket = 1 THEN.
Question 5
A SET lives TO 3
B IF lives > 0 THEN
C IF lives > 1 THEN
D SAY "Go"
E END REPEAT
F END IF
Line C — Line 3 opens an IF, but it is closed by END REPEAT. The inner block should be REPEAT lives TIMES so the END REPEAT matches it.

Student with a class code? Join your class →