Tracing nested loops

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Tracing nested loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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.
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
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

nested loop

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
for i from 1 to 3 for _ from 1 to 2 print i, j
2.
for i from 1 to 3 for j from 1 to 4 count ← count _ 1
3.
for i from 1 to 2 for j from 1 __ 3 print i, j
4.
for i from 1 to 3 for j from 1 to _ print j
5.
for a from 1 to 2 for b from 1 to 3 _____ ← count + 1

Spot the Error

Shade in the letter of the line that contains an 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
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
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 "*"
Question 4
A for day 1 to 3
B for game from 1 to 2
C print day, game
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

Answer Key

Tracing nested loops
Section A: Quick Questions
Q1
B — 12 times
Q2
A — The inner loop finishes all its repeats before the outer loop moves to its next value.
Q3
A — 1 1, 1 2, 1 3
Q4
A — j, the inner loop variable
Q5
B — 10 times
Section B: Match the Pairs
Q1
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
Q2
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
Q3
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
Q4
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)
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
for i from 1 to 3 for j from 1 to 2 print i, j
Q2
for i from 1 to 3 for j from 1 to 4 count ← count + 1
Q3
for i from 1 to 2 for j from 1 to 3 print i, j
Q4
for i from 1 to 3 for j from 1 to i print j
Q5
for a from 1 to 2 for b from 1 to 3 count ← count + 1
Section E: Spot the Error
Q1
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'.
Q2
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'.
Q3
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'.
Q4
A — The for loop is missing the keyword 'from'. The outer loop should be 'for day from 1 to 3'.
Q5
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'.