Skip to content
IGCSE·Tuition
Computer Science · Lesson

Trace assignment and comparison in pseudocode

One line of pseudocode can look like a sum from maths class, and that is where trace answers start to go wrong.

On this page
  1. How do I read each line?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

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
LineAB
A ← 44
B ← 747
A ← A + B117
B ← A - B114
A ← A - B74

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

Questions people ask

Why is Total ← Total + 1 not a false equation?

The arrow is an instruction, not a statement about equality. It means: work out the right side using the current values, then store the result in the variable on the left. After the line runs, Total holds a new value, one larger than before.

How do I show an unchanged variable in a trace table?

Carry the value down into the next row, or leave the cell blank only if your teacher accepts that convention. Writing the value again is safest, because it shows the variable still exists and has not been reset.

What does a comparison produce?

A comparison such as Score = 50 produces TRUE or FALSE. It does not change any variable. The IF or WHILE that surrounds it uses that TRUE or FALSE to decide which path the program takes.

Updated:

Your next step

If your trace tables keep ending with the wrong value, a one-to-one teacher can watch you work through a line and catch the moment you read an update as an equation.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parents: enquire here

  • 9,000+ students helped through our service
  • 9+ years helping IGCSE students