← ACARA Digital Technologies Years 7–8

Tracing a decision inside a loop

Trace an IF inside a loop with one row for each pass, showing the condition result (true or false) and any variables that change. The final row gives the output.

Understanding Tracing a decision inside a loop in ACARA Digital Technologies Years 7–8

What it does: The condition n > 2 is tested once per pass, giving a different result in each row: false, false, true, true. The OUTPUT line runs after the loop ends, so the final row shows the finished value of total, 9. Give each pass of the loop its own row. This lets you see exactly what changed each time. 3 is greater than 2, so the condition is TRUE. The IF block runs and count goes from 0 to 1. A variable keeps its value when the IF block is skipped. Copy the old value down so the next row starts correctly.

Common mistakes: The counter must start at 0. Starting at 1 means the final row shows 3 instead of the correct 2 (only i = 4 and i = 5 pass the test). It should be count ← 0. With < 5 the loop stops after pass 4, so one row is missing from the trace. It should be WHILE pass <= 5 so that pass 5 also gets its own row.

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 420–481) and word search (Modules 420–519).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A loop runs 5 times and has an IF inside it. How many rows does the trace table need for the loop?
1 row for the whole loop5 rows, one for each pass
Matches
One row for a loop that runs 4 times, showing only the final count
↓
The trace skips the passes. Each repeat needs its own row showing the condition result and changed variables.
Applying
Meanings
Term
one row per pass
Meaning
Trace an IF inside a loop with one row for each pass, showing the condition result (true or false) and any variables that change. The final row gives the output.
Practice
pass | n | n > 2 | total
Testing
Gaps
n ← 1 TO 4
Check Answer
Spots
1count ← 1
2FOR i ← 1 TO 5

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

The 'Hermit Crab' badge — Definition Collector, 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 runs 5 times and has an IF inside it. How many rows does the trace table need for the loop?
A. 5 rows, one for each pass ✓
B. 1 row for the whole loop
Give each pass of the loop its own row. This lets you see exactly what changed each time.
2. Which is the best thing to record in the condition column on each pass?
A. TRUE or FALSE for that pass ✓
B. The final answer of the whole algorithm
The condition column shows whether the IF test was true or false on that pass only. The final answer belongs in the last row.
3. Trace: FOR n ← 1 TO 4, IF n > 2 THEN count ← count + 1. What does the row for n = 2 show?
A. n = 2, n > 2 is TRUE, count goes up by 1
B. n = 2, n > 2 is FALSE, count does not change ✓
2 is not greater than 2, so the condition is FALSE. The count step is skipped on this pass.
4. Trace: FOR n ← 1 TO 4, IF n > 2 THEN count ← count + 1. What does the row for n = 3 show?
A. n = 3, n > 2 is TRUE, count becomes 1 ✓
B. n = 3, n > 2 is FALSE, count stays 0
3 is greater than 2, so the condition is TRUE. The IF block runs and count goes from 0 to 1.
5. A student traces a loop with an IF inside it. When should the OUTPUT be written in the table?
A. In every row, even if nothing is displayed
B. In the final row, after the loop has finished ✓
Only write OUTPUT when the algorithm actually displays something. If the display step comes after the loop, it goes in the final row.

Match the Pairs

REPEAT 5 TIMES -> 5 rows → A loop that repeats 5 times needs one row in the trace table for each pass, so the table has 5 rows.
i=3 | i>2 true | count=2 → On pass 3 the condition is true, so the count variable changes and the new value is written in that row.
The IF condition is checked on every pass, so each row records true or false for that pass. → FOR n = 1 TO 4: IF n > 2 -> F, F, T, T
After the last pass, the final row gives the output once the loop has finished. → OUTPUT total (last row: total = 9)
One row for a loop that runs 4 times, showing only the final count → The trace skips the passes. Each repeat needs its own row showing the condition result and changed variables.
Pass 2: n=2, count=1 (no condition result shown) → The row does not show whether the IF condition was true or false, so we cannot tell why count did or did not change.

Key Term

one row per pass
Trace an IF inside a loop with one row for each pass, showing the condition result (true or false) and any variables that change. The final row gives the output.

Fill in the Blanks

1. total ← 0 FOR n ← 1 TO 4 IF n > 2 THEN total ← total + n
Hint: Type the keyword that starts a counted loop, so the IF is tested once on every pass.
2. IF n > 2 THEN total ← total + n Pass 2: n = 2, n > 2 is false, total = 0
Hint: Type the condition result for this row. Is 2 greater than 2?
3. IF n > 2 THEN total ← total + n Pass 3: n = 3, true, total = 3 Pass 4: n = 4, true, total = 7
Hint: Type the name of the variable that changes in this row when the IF is true.
4. Pass 4: n = 4, true, total = 7 OUTPUT total Final row: output is 7
Hint: Type the keyword that shows a value to the user. The final row of the trace records this.
5. count ← 0 FOR n ← 1 TO 4 IF n < 3 THEN count ← count + 1
Hint: Type the comparison operator meaning less than, so only passes 1 and 2 are true.

Spot the Error

Question 1
A count ← 1
B FOR i ← 1 TO 5
C IF i > 3 THEN
D count ← count + 1
E OUTPUT count
Line A — The counter must start at 0. Starting at 1 means the final row shows 3 instead of the correct 2 (only i = 4 and i = 5 pass the test). It should be count ← 0.
Question 2
A pass ← 1
B WHILE pass < 5
C IF pass = 3 THEN
D OUTPUT "three"
E pass ← pass + 1
Line B — With < 5 the loop stops after pass 4, so one row is missing from the trace. It should be WHILE pass <= 5 so that pass 5 also gets its own row.
Question 3
A passes ← 0
B FOR EACH mark IN marks
C IF mark <= 50 THEN
D passes ← passes + 1
E OUTPUT passes
Line C — The relational operator is the wrong way round, so the condition is true for low marks instead of passing marks. It should be IF mark >= 50 THEN.
Question 4
A total ← 0
B FOR i ← 1 TO 5
C IF i ≠ 3 THEN
D total ← i
E OUTPUT total
Line D — This replaces total on every true pass instead of adding to it, so the running total is lost. It should be total ← total + i.
Question 5
A Pass 1: i=1, i>2 false, c=0
B Pass 2: i=2, i>2 false, c=0
C Pass 3: i=3, i>2 false, c=1
D Pass 4: i=4, i>2 true, c=2
E Output: 2
Line C — 3 > 2 is true, so the condition result in this row must be true. The row shows c changing to 1, which only happens when the condition is true. It should say i>2 true.

Student with a class code? Join your class →