Unit 3: Programming · Math

Article

Tracing variables step by step

7 min read

Chess players do not remember every piece with their eyes closed. They look at the board. When a program gets longer than a few lines, you cannot keep every value in your head either. So write the board down. That is called tracing, and it is the most important programming skill on the AP exam.

A trace table, step by step

To trace a program means to follow it as the computer does and record what every variable holds. You record it in a trace table. Here is the method:

  1. Make one column for each variable.
  2. Make one row for each statement, from the top to the bottom.
  3. In each row, work out the right-hand side using the values from the row above. Write the answer in the column of the variable on the left.
  4. Copy the other variables down unchanged. Only the variable that was assigned can change.

Let us trace this program:

AP pseudocode
a ← 6
b ← a MOD 4
a ← a + b
b ← b * a
DISPLAY(b)
Runs in your browser
After this statementabHow it was worked out
a ← 66
b ← a MOD 4626 MOD 4 is 2
a ← a + b826 + 2
b ← b * a8162 * 8, using the new a

The last statement uses the new a (8), not the old one (6), because a was already changed. The program shows 16.

Check your understanding

What does this program display?

AP pseudocode
x ← 10
y ← x MOD 3
x ← x - y
y ← y + x
DISPLAY(y)

A longer trace with output

When a program uses DISPLAY, add an Output column. Each DISPLAY adds its value to the end of it. On the exam, values shown by DISPLAY appear in order with a space after each one.

AP pseudocode
Line 1: n ← 20
Line 2: r ← n MOD 7
Line 3: DISPLAY(r)
Line 4: n ← n - r
Line 5: DISPLAY(n)
Line 6: n ← n MOD 5
Line 7: DISPLAY(n + r)
After linenrOutput so far
120
2206
32066
41466
51466 14
6466 14
7466 14 10

Line 2: 20 MOD 7 is 6, because 7 goes into 20 twice (14), with 6 left over. Line 4: n becomes 20 - 6 = 14. Line 6: 14 MOD 5 is 4. Line 7 shows n + r, which is 4 + 6 = 10. The program shows 6 14 10.

Notice two things. A DISPLAY does not change any variable, and a line that changes one variable leaves the others alone.

Check your understanding

What is displayed when this program runs? (Each DISPLAY shows its value followed by a space.)

AP pseudocode
n ← 15
DISPLAY(n MOD 6)
n ← n - 6
DISPLAY(n MOD 6)
DISPLAY(n)

Working backwards

Sometimes the exam gives the result and asks about the start. Then trace backwards, from the bottom, and undo each statement with the opposite operation.

The first line of this program is hidden. It gave x its starting value. The program shows 11. What was the starting value?

x ← ???
x ← x * 3
x ← x - 4
DISPLAY(x)

Undo the steps from the end. Before x ← x - 4, x was 11 + 4 = 15. Before x ← x * 3, x was 15 / 3 = 5. So the hidden line was x ← 5. Always check by tracing forwards again: 5, then 15, then 11.

Worked example: a swap with no temporary variable

Earlier you swapped two values with a temp variable. With numbers, you can also swap with arithmetic. Trace this program, where a starts as 3 and b as 8:

AP pseudocode
a ← 3
b ← 8
a ← a + b
b ← a - b
a ← a - b
After this statementabHow it was worked out
a ← 33
b ← 838
a ← a + b1183 + 8
b ← a - b11311 - 8
a ← a - b8311 - 3

At the end a is 8 and b is 3, so the values are swapped. The trick works because a temporarily holds the total of both numbers, and subtracting one number leaves the other. You do not have to remember the trick. You only have to trace it correctly, and the table does that for you.

Common mistakes

  • Using the old value of a variable that has already changed. In each row, use the numbers from the row above, and update only the variable on the left.
  • Changing a variable that was not assigned. Only the variable on the left of the arrow changes.
  • Putting the output in the variable columns. DISPLAY adds to the output. It does not change variables.
  • Skipping rows to save time. Skipped rows are where most wrong answers come from. Write every row until you are very fast.
  • Mixing up MOD and division. 20 MOD 7 is 6, not 2 and not 2.86.

On the exam

Most programming questions are tracing questions: "What is displayed as a result of running the code segment?" or "What is the value of x after the code runs?" Copy the code's variables into a small table in your notes, then trace line by line. Questions may also give the result and ask what a missing value or a missing line must be, so practise working backwards as well.

Key words

Trace: follow a program statement by statement, recording what every variable holds.

Trace table: a table with a column for each variable and a row for each statement.

Output: what a program shows with DISPLAY.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.