Tracing a decision inside a loop with a trace table

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

Tracing a decision inside a loop with a trace table

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 One row only, for the whole loop
B One row for each repeat, so 5 rows
2. In a trace table for a loop with an IF, what do you record in the condition column?
A Whether the IF condition was true or false on that repeat
B The final answer the algorithm will print
3. Row 3 of a trace table: count = 4, the IF condition (n > 5) is false, and the IF only changes total. What should you do with count in row 3?
A Copy the value 4 down because the branch did not change it
B Leave the cell empty or reset it to 0
4. Which is the correct way to fill in a trace table row when the IF condition is true and the branch says: SET score TO score + 10?
A Write new values for every variable in the table
B Write the new score and copy every other variable down unchanged
5. An algorithm uses a loop with IF number MOD 2 = 0 THEN DISPLAY number. What does the output column show for a repeat where the condition is false?
A Nothing, because the DISPLAY step did not run
B The number anyway, because the loop still ran

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 4 / IF i > 2 THEN / total ← total + i AThe condition is false, so the IF branch is skipped. Total should be copied down as 3, not changed to 5.
2Output column Bi = 3 | i > 2 | true
3Copy unchanged values down: if the branch does not update a variable, write the same value in the next row. CA loop that only adds to total when the condition is true. Rows 1 and 2 leave total unchanged, and rows 3 and 4 change it.
4Record the condition result in its own column for each repeat, as true or false, before updating any variables. DThe column where you write anything the algorithm displays on that repeat, leaving it blank when nothing is shown.
5Row 2: i = 2, i > 2 is false, total changes from 3 to 5 Ecount: 0, 0, 1, 1, 2
6Stop tracing after the first row because the IF was false FA trace must continue with one row for every loop repeat until the loop ends, even when the IF is false on some rows.

Definitions

trace table

Fill in the Blanks

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

1.
Rule: IF n > 2 THEN total ← total + n Row 1: n = 1, so the condition n > 2 is _____
2.
Rule: IF n > 2 THEN total ← total + n Row 3: n = 3, total before = 0 total after = 0 + _
3.
Rule: IF n > 2 THEN total ← total + n Row 4: n = 2, n > 2 is false total before = 5, total after = _
4.
IF n MOD 2 = 0 THEN ______ n Row n = 4: output column shows 4
5.
Final row: count = 3, total = 9 Final values are read from the ____ row

Spot the Error

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

Question 1
A n | even? | total | output
B 1 | No | 0 | -
C 2 | Yes | 3 | -
D 3 | No | 2 | -
E 4 | Yes | 6 | -
Question 2
A n | n > 3? | score
B 1 | No | 0
C 2 | Yes | 0
D 3 | No | 0
E 4 | Yes | 4
F 5 | Yes | 9
Question 3
A SET total TO n
B FOR n FROM 1 TO 4
C IF n > 2 THEN
D SET total TO total + n
E END IF
F END FOR
Question 4
A FOR n FROM 1 4
B IF n > 2 THEN
C OUTPUT n
D END IF
E END FOR
Question 5
A n | n is even? | evens
B 1 | No | 0
C 2 | Yes | 1
D 3 | No | 2
E 4 | Yes | 2
F Final evens = 2

Answer Key

Tracing a decision inside a loop with a trace table
Section A: Quick Questions
Q1
B — One row for each repeat, so 5 rows
Q2
A — Whether the IF condition was true or false on that repeat
Q3
A — Copy the value 4 down because the branch did not change it
Q4
B — Write the new score and copy every other variable down unchanged
Q5
A — Nothing, because the DISPLAY step did not run
Section B: Match the Pairs
Q1
FOR i FROM 1 TO 4 / IF i > 2 THEN / total ← total + i → A loop that only adds to total when the condition is true. Rows 1 and 2 leave total unchanged, and rows 3 and 4 change it.
Q2
Output column → The column where you write anything the algorithm displays on that repeat, leaving it blank when nothing is shown.
Q3
Copy unchanged values down: if the branch does not update a variable, write the same value in the next row. → count: 0, 0, 1, 1, 2
Q4
Record the condition result in its own column for each repeat, as true or false, before updating any variables. → i = 3 | i > 2 | true
Q5
Row 2: i = 2, i > 2 is false, total changes from 3 to 5 → The condition is false, so the IF branch is skipped. Total should be copied down as 3, not changed to 5.
Q6
Stop tracing after the first row because the IF was false → A trace must continue with one row for every loop repeat until the loop ends, even when the IF is false on some rows.
Section C: Definitions
Q1
trace table — A table with one row per loop repeat. Record the IF condition result, change only the variables the branch updates, copy the rest down, and note any output.
Section D: Fill in the Blanks
Q1
Rule: IF n > 2 THEN total ← total + n Row 1: n = 1, so the condition n > 2 is false
Q2
Rule: IF n > 2 THEN total ← total + n Row 3: n = 3, total before = 0 total after = 0 + 3
Q3
Rule: IF n > 2 THEN total ← total + n Row 4: n = 2, n > 2 is false total before = 5, total after = 5
Q4
IF n MOD 2 = 0 THEN OUTPUT n Row n = 4: output column shows 4
Q5
Final row: count = 3, total = 9 Final values are read from the last row
Section E: Spot the Error
Q1
C — This trace is for total starting at 0, adding n only when n is even. At n = 2 the condition is Yes, so total = 0 + 2 = 2, not 3.
Q2
C — When n is 2, the condition n > 3 is false, so the result column must say No. The row wrongly says Yes, but the score did not change.
Q3
A — total must be set to 0 before the loop, so the first trace row starts from a known value. n has no value yet, so it should be SET total TO 0.
Q4
A — A FOR loop needs the word TO between the start and end values. It should be FOR n FROM 1 TO 4, which gives four rows in the trace table.
Q5
D — At n = 3 the condition is No, so the branch is skipped and evens is not changed. The unchanged value 1 must be copied down, not changed to 2.