← VCE Software Development U3O1

Tracing nested loops

A loop inside another loop. When tracing, the inner loop completes all its repeats for each single repeat of the outer loop, so total passes = outer repeats x inner repeats.

Understanding Tracing nested loops in VCE Software Development U3O1

What it does: i stays at 1 while j goes 1, 2, 3. Only after the inner loop finishes does i change to 2 and j restart at 1. The outer loop runs 6 times and the inner loop runs 2 times for each, so the print line runs 6 x 2 = 12 times. Correct trace: (1,1) (1,2) (1,3) then (2,1) (2,2) (2,3). That is 6 passes, not 3.

Common mistakes: The comment says there are 3 rows, so the outer loop must run 3 times. With 'to 2' it only runs twice, so row 3 is never traced. It should be 'for row from 1 to 3'. A counter must start at 0 before the loops. Starting at 1 makes the final count 13 instead of 4 x 3 = 12. It should be 'passes <- 0'.

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 168–177) and word search (Modules 168–193).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A nested loop has an outer loop that runs 3 times and an inner loop that runs 4 times. How many times does the inner loop body run in total?
12 times7 times
Matches
for i from 1 to 2 for j from 1 to 3 print i, j Trace: (1,1) (2,2) (1,3)
↓
The trace changes the outer and inner variables together. The inner loop must finish all of j = 1 to 3 before i changes to 2.
Applying
Meanings
Term
nested loop
Meaning
A loop inside another loop. When tracing, the inner loop completes all its repeats for each single repeat of the outer loop, so total passes = outer repeats x inner repeats.
Practice
count <- 0
Testing
Gaps
for from 1 to 2
Check Answer
Spots
1// 3 rows with 2 seats each
2for row from 1 to 2

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

The 'Void Whale' badge — Definition Drifter, 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 nested loop has an outer loop that runs 3 times and an inner loop that runs 4 times. How many times does the inner loop body run in total?
A. 7 times
B. 12 times ✓
The inner loop runs 4 times for each of the 3 outer repeats, so 3 x 4 = 12. You multiply the repeats, you don't add them.
2. When tracing a nested loop, which statement is correct?
A. The inner loop finishes all its repeats before the outer loop moves to its next value. ✓
B. The outer and inner loops take turns, moving one step each.
The inner loop completes every repeat for each single repeat of the outer loop. The loops do not alternate.
3. Look at this pseudocode: for i from 1 to 2 for j from 1 to 3 print i, j What are the first three pairs printed?
A. 1 1, 1 2, 1 3 ✓
B. 1 1, 2 1, 1 2
While i is 1, j counts 1, 2, 3. Only after that does i change to 2.
4. In the same code (for i from 1 to 2, for j from 1 to 3), which variable changes most quickly in a trace table?
A. j, the inner loop variable ✓
B. i, the outer loop variable
The inner variable j changes on every pass. The outer variable i only changes after j has finished its whole cycle.
5. for a from 1 to 5 for b from 1 to 2 print "Hi" How many times is "Hi" printed?
A. 5 times
B. 10 times ✓
The outer loop runs 5 times and the inner loop runs 2 times each time, so 5 x 2 = 10 prints.

Match the Pairs

for i from 1 to 3 for j from 1 to 2 print i, j → The inner loop runs j = 1, 2 in full for every value of i, so the print line runs 6 times in total
for a from 1 to 4 for b from 1 to 5 count <- count + 1 → After tracing the whole algorithm, count ends at 20 because the inner loop completes 5 passes for each of the 4 outer passes
In a trace table, the outer variable stays the same while the inner variable changes through all its values → i=1, j=1 i=1, j=2 i=1, j=3 i=2, j=1
To count total passes of a nested loop, multiply the outer repeats by the inner repeats → for x from 1 to 6 for y from 1 to 2 print x, y (6 x 2 = 12 passes)
for i from 1 to 2 for j from 1 to 3 print i, j Trace: (1,1) (2,2) (1,3) → The trace changes the outer and inner variables together. The inner loop must finish all of j = 1 to 3 before i changes to 2.
for i from 1 to 4 for j from 1 to 3 print i, j Total passes = 4 + 3 = 7 → Total passes were found by adding the loop counts. Nested loops need the repeats multiplied, not added.

Key Term

nested loop
A loop inside another loop. When tracing, the inner loop completes all its repeats for each single repeat of the outer loop, so total passes = outer repeats x inner repeats.

Fill in the Blanks

1. for i from 1 to 3 for j from 1 to 2 print i, j
Hint: Type the variable name used by the inner loop, the one that is printed after i.
2. for i from 1 to 3 for j from 1 to 4 count ← count + 1
Hint: Type the operator that adds one to the counter each time the inner loop runs.
3. for i from 1 to 2 for j from 1 to 3 print i, j
Hint: Type the keyword that sets the last value of the inner loop.
4. for i from 1 to 3 for j from 1 to i print j
Hint: Type the outer loop variable so the inner loop repeats as many times as the outer loop's current value.
5. for a from 1 to 2 for b from 1 to 3 count ← count + 1
Hint: Type the name of the variable that keeps track of the total passes.

Spot the Error

Question 1
A // 3 rows with 2 seats each
B for row from 1 to 2
C for seat from 1 to 2
D print row, seat
Line B — The comment says there are 3 rows, so the outer loop must run 3 times. With 'to 2' it only runs twice, so row 3 is never traced. It should be 'for row from 1 to 3'.
Question 2
A // 4 outer x 3 inner passes
B passes <- 1
C for i from 1 to 4
D for j from 1 to 3
E passes <- passes + 1
Line B — A counter must start at 0 before the loops. Starting at 1 makes the final count 13 instead of 4 x 3 = 12. It should be 'passes <- 0'.
Question 3
A // 2 rows, 5 stars in each row
B for row from 1 to 2
C for star from 1 to row
D print "*"
Line C — The inner loop should run 5 times for every row. Using 'row' as the end value makes it run 1 time, then 2 times. It should be 'for star from 1 to 5'.
Question 4
A for day 1 to 3
B for game from 1 to 2
C print day, game
Line A — The for loop is missing the keyword 'from'. The outer loop should be 'for day from 1 to 3'.
Question 5
A total <- 0
B for i from 1 to 3
C for i from 1 to 4
D total <- total + 1
E print total
Line C — The inner loop reuses the outer variable i, so the two loops clash when you trace i. The inner loop needs its own variable: 'for j from 1 to 4'.

Student with a class code? Join your class →