Assignment stores a value in a variable. Comparison asks a question and gives TRUE or FALSE. In a trace, assignment changes a column and comparison only changes the path you follow.
This skill sits in pseudocode for the applicable syllabus and follows on from the notation lesson. It underpins every trace table question you will meet.
How do I read each line?
Use two questions for an assignment such as A ← A + B. First: “what are the current values on the right?” Second: “which variable receives the answer?” The right side is worked out first, using the values the variables hold at that moment.
For a comparison such as IF A = B THEN: “is it TRUE or FALSE right now?” Then follow the matching branch. No variable changes.
A trace table with one column per variable and one row per executed line keeps this honest.
Worked example
Trace this algorithm and give the output.
A ← 4
B ← 7
A ← A + B
B ← A - B
A ← A - B
OUTPUT A, B
| Line | A | B |
|---|---|---|
| A ← 4 | 4 | |
| B ← 7 | 4 | 7 |
| A ← A + B | 11 | 7 |
| B ← A - B | 11 | 4 |
| A ← A - B | 7 | 4 |
Line 3: A becomes 4 + 7 = 11. Line 4: B becomes 11 - 7 = 4.
Line 5: A becomes 11 - 4 = 7. The output is 7 4, so the two values have swapped.
Checking again by the first values: A started as 4 and B as 7, and they finish as 7 and 4. The algorithm swaps without a third variable.
The mistake to watch for
A common slip is to use an old value instead of the latest one, or to think a variable changes when a comparison runs.
Mistaken trace: At
B ← A - Bthe student used A = 4 (the old value) and wrote B = 4 - 7 = -3.Line 3 had already changed A to 11, so the current value of A is 11.
The correction is to read the row above, not the starting values. Always take the latest value in each column. A second slip is writing Score ← 50 where the question needs the test Score = 50, which changes the variable instead of checking it.
Check yourself
1. Trace: X ← 10, Y ← X, X ← X * 2, OUTPUT Y.
Show answer
Y takes the value 10 when it is assigned. Changing X afterwards does not change Y. Output: 10.
2. After Count ← 5 and then two lines Count ← Count - 1, what is Count?
Show answer
5 - 1 = 4, then 4 - 1 = 3. Count is 3.
3. What does Score = 50 give when Score holds 50, and does Score change?
Show answer
It gives TRUE, and Score stays 50. A comparison never changes a variable.
Where this leads next
Now apply the same careful reading to input checks in describing input validation. You can step through this swap in the restricted pseudocode trace trainer.
If trace tables still feel slow or error-prone, a teacher can work through your own past mistakes with you in online one-to-one Computer Science tuition.