← Years 5–6 Creating Digital Solutions

Branching inside a loop

An IF placed inside a REPEAT is checked again on every repetition, so each pass through the loop can take a different path.

Understanding Branching inside a loop in Years 5–6 Creating Digital Solutions

What it does: If the light changes between passes, the IF picks stop on one pass and walk on another. The IF decides what happens on each pass, and the loop ends when the bag is full. In the correct answer the IF is indented under REPEAT, so it is inside the loop. In the other one the IF sits before the loop and is only checked once. Indented lines belong to the REPEAT above them. So the IF runs on every repeat. The IF is checked each time. Pass 2 is false, so the robot skips cleaning on that pass. The condition can be true on one pass and false on the next, so the path can change.

Common mistakes: Now both paths do the same thing, so the IF makes no difference. When the light is red the car should stop. The line should be: stop the car. We take apples out, so the bag gets emptier, not fuller. The loop would never stop. It should be: REPEAT UNTIL the bag is empty.

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 21–30) and word search (Modules 21–40).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A student writes an IF inside a REPEAT. How many times is the IF checked?
Only once, on the first repeatEvery time the loop repeats
Matches
REPEAT 3 TIMES IF ball is red THEN throw ball
↓
Nothing is indented, so it is unclear that the IF is inside the REPEAT. Indent the IF and the step under it.
Applying
Meanings
Term
IF inside REPEAT
Meaning
An IF placed inside a REPEAT is checked again on every repetition, so each pass through the loop can take a different path.
Practice
REPEAT 5 times
Testing
Gaps
light is red THEN
Check Answer
Spots
2 IF the light is red THEN
3 drive forward

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

The 'Walrus' badge — Method Master, 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 student writes an IF inside a REPEAT. How many times is the IF checked?
A. Every time the loop repeats ✓
B. Only once, on the first repeat
The IF is part of the loop body, so it is checked again on every pass. Each pass can take a different path.
2. Which algorithm shows an IF inside a REPEAT?
A. IF the light is red THEN stop REPEAT 5 times walk forward
B. REPEAT 5 times IF the light is red THEN stop ✓
In the correct answer the IF is indented under REPEAT, so it is inside the loop. In the other one the IF sits before the loop and is only checked once.
3. What does the indenting tell you in this algorithm? REPEAT 4 times IF the cup is empty THEN fill it
A. The IF is inside the loop ✓
B. The IF happens after the loop ends
Indented lines belong to the REPEAT above them. So the IF runs on every repeat.
4. A robot does this: REPEAT 3 times, IF the floor is dirty THEN clean it. The floor is dirty on pass 1, clean on pass 2, dirty on pass 3. What happens?
A. It cleans on pass 1 and pass 3 only ✓
B. It cleans on all 3 passes
The IF is checked each time. Pass 2 is false, so the robot skips cleaning on that pass.
5. Why can each pass through a loop with an IF inside it take a different path?
A. The loop writes a new algorithm each time
B. The condition is checked again each time and might change ✓
The condition can be true on one pass and false on the next, so the path can change.

Match the Pairs

REPEAT 5 TIMES IF tired THEN rest → The IF is indented inside the REPEAT, so the check happens on every one of the 5 repetitions
REPEAT UNTIL door is open IF door is locked THEN use key → The loop keeps going until the door opens, and each pass decides whether the key is needed
Each pass through the loop can take a different path because the IF is checked every time → REPEAT 4 TIMES IF light is red THEN stop ELSE walk
A loop that stops when a decision is met → REPEAT UNTIL bag is full IF fruit is ripe THEN pick fruit
REPEAT 3 TIMES IF ball is red THEN throw ball → Nothing is indented, so it is unclear that the IF is inside the REPEAT. Indent the IF and the step under it.
IF dog is hungry THEN REPEAT 3 TIMES feed dog → The REPEAT is inside the IF, so the hungry check happens only once. The IF should be inside the REPEAT to be checked each time.

Key Term

IF inside REPEAT
An IF placed inside a REPEAT is checked again on every repetition, so each pass through the loop can take a different path.

Fill in the Blanks

1. REPEAT 5 TIMES IF light is red THEN stop
Hint: Type the keyword that makes a decision on every pass through the loop
2. REPEAT 7 TIMES IF door is open THEN walk through
Hint: Type the keyword that comes after the condition and before the action
3. REPEAT UNTIL bag is full IF fruit is an apple THEN add it to bag
Hint: Type the keyword that makes the loop run until a condition becomes true
4. REPEAT UNTIL seat found IF seat is free THEN sit down ELSE walk on
Hint: Type the keyword for the other path taken when the IF condition is false
5. REPEAT 10 TIMES IF score > 5 THEN add 1 to points
Hint: Type the comparison symbol that checks if a number is greater than another

Spot the Error

Question 1
A REPEAT 5 TIMES
B IF the light is red THEN
C drive forward
D ELSE
E drive forward
Line C — Now both paths do the same thing, so the IF makes no difference. When the light is red the car should stop. The line should be: stop the car.
Question 2
A REPEAT UNTIL the bag is full
B take out one apple
C IF the apple is bad THEN
D put it in the bin
Line A — We take apples out, so the bag gets emptier, not fuller. The loop would never stop. It should be: REPEAT UNTIL the bag is empty.
Question 3
A REPEAT 10 TIMES
B IF the ball is hit
C add 1 to score
D END REPEAT
Line B — An IF needs the word THEN at the end of the condition line. It should be: IF the ball is hit THEN.
Question 4
A REPEAT 8 TIMES
B move forward
C REPEAT there is a wall THEN
D turn right
Line C — A decision inside a loop needs the word IF, not REPEAT. The robot should check IF there is a wall THEN on every repeat.
Question 5
A SET count TO 0
B REPEAT 6 TIMES
C IF the card is a star THEN
D SET count TO 1
E SAY count
Line D — This resets count to 1 every time a star is found, so it can never go higher than 1. It should add to the count: ADD 1 TO count.

Student with a class code? Join your class →