Tracing a decision inside a loop

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

Tracing a decision inside a loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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

Match the Pairs

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

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

Definitions

one row per pass

Fill in the Blanks

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

1.
total ← 0 ___ n ← 1 TO 4 IF n > 2 THEN total ← total + n
2.
IF n > 2 THEN total ← total + n Pass 2: n = 2, n > 2 is _____, total = 0
3.
IF n > 2 THEN total ← total + n Pass 3: n = 3, true, total = 3 Pass 4: n = 4, true, _____ = 7
4.
Pass 4: n = 4, true, total = 7 ______ total Final row: output is 7
5.
count ← 0 FOR n ← 1 TO 4 IF n _ 3 THEN count ← count + 1

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A count ← 1
B FOR i ← 1 TO 5
C IF i > 3 THEN
D count ← count + 1
E OUTPUT count
Question 2
A pass ← 1
B WHILE pass < 5
C IF pass = 3 THEN
D OUTPUT "three"
E pass ← pass + 1
Question 3
A passes ← 0
B FOR EACH mark IN marks
C IF mark <= 50 THEN
D passes ← passes + 1
E OUTPUT passes
Question 4
A total ← 0
B FOR i ← 1 TO 5
C IF i ≠ 3 THEN
D total ← i
E OUTPUT total
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

Answer Key

Tracing a decision inside a loop
Section A: Quick Questions
Q1
A — 5 rows, one for each pass
Q2
A — TRUE or FALSE for that pass
Q3
B — n = 2, n > 2 is FALSE, count does not change
Q4
A — n = 3, n > 2 is TRUE, count becomes 1
Q5
B — In the final row, after the loop has finished
Section B: Match the Pairs
Q1
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.
Q2
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.
Q3
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
Q4
After the last pass, the final row gives the output once the loop has finished. → OUTPUT total (last row: total = 9)
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
total ← 0 FOR n ← 1 TO 4 IF n > 2 THEN total ← total + n
Q2
IF n > 2 THEN total ← total + n Pass 2: n = 2, n > 2 is false, total = 0
Q3
IF n > 2 THEN total ← total + n Pass 3: n = 3, true, total = 3 Pass 4: n = 4, true, total = 7
Q4
Pass 4: n = 4, true, total = 7 OUTPUT total Final row: output is 7
Q5
count ← 0 FOR n ← 1 TO 4 IF n < 3 THEN count ← count + 1
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
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.
Q5
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.