Skip to content
IGCSE·Tuition

Computer Science · Lessons

Connect a trace with the final output

The trace table is complete, yet the final answer you write is still the wrong variable.

On this page
  1. How do you link the table to the output?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

A trace table records the value of each variable as an algorithm runs. To connect it with the output, find the variable named in the OUTPUT statement and read its value from the row where the program reaches that statement. Then explain why the loop ended there.

This lesson belongs to the problem-solving explanations module. It builds on explaining the purpose of a variable, since you need to know which variable is the answer, and on tracing from algorithm design.

  1. Build the table with one column per variable and one row per update.
  2. Mark the row where the loop test fails. The loop ends at the first test that is false.
  3. Check the OUTPUT statement. Note which variable it names.
  4. Read that variable’s final value from the last row that changed it.
  5. Write one sentence saying which variable, what value, and why the loop stopped.

Worked example

Total ← 0
Number ← 10
WHILE Number > 0 DO
   Total ← Total + Number
   Number ← Number - 3
ENDWHILE
OUTPUT Total
PassNumber > 0 ?Total afterNumber after
Start010
110 > 0 true107
27 > 0 true174
34 > 0 true211
41 > 0 true22−2
Test−2 > 0 false22−2

Explanation. The loop adds 10, 7, 4 and 1 to Total, giving 10 + 7 + 4 + 1 = 22. After pass 4, Number is −2, so the test −2 > 0 is false and the loop ends. The value −2 is never added. The statement OUTPUT Total prints 22.

Double check. The numbers added are 10, 7, 4, 1 (each 3 less than the one before, while still above 0). Their sum is 22.

The mistake to watch for

A common slip is to read the wrong column or the wrong row:

The output is −2 because that is the last value in the table.

−2 is the last value of Number, but the OUTPUT statement names Total. A second version of the slip is giving 21 or 20 by stopping the trace one pass early or adding −2 by mistake.

The correction: look at the statement first. It says OUTPUT Total, so read the Total column. Its last value is 22.

Check yourself

1. Trace the code and state the output with one sentence of explanation.

A ← 2
B ← 1
REPEAT
   B ← B * A
   A ← A + 1
UNTIL A > 4
OUTPUT B
Show answer

Start A = 2, B = 1. Pass 1: B = 1 × 2 = 2, A = 3, test 3 > 4 false. Pass 2: B = 2 × 3 = 6, A = 4, test 4 > 4 false. Pass 3: B = 6 × 4 = 24, A = 5, test 5 > 4 true so the loop stops. The output is 24, because B was last updated in pass 3 and OUTPUT B prints that value.

2. In the worked example, which row of the table gives the output, and why not the Test row?

Show answer

Pass 4 is the last row that changes Total, giving 22. The Test row only checks the condition, and the loop body is not run, so Total stays at 22. Both rows show 22 in the Total column, but pass 4 is where the value was produced.

3. Change the starting value of Number in the worked example to 9. What is the output?

Show answer

Number runs 9, 6, 3 and then 0. Total becomes 9, 15, 18. After pass 3, Number is 0, and 0 > 0 is false, so the loop stops. The output is 18 (9 + 6 + 3 = 18).

Where this leads next

The last skill in this module is shrinking a failing program to its smallest form in preparing a minimal reproducible learning example. The restricted pseudocode trace trainer lets you check your own table against a stepped trace.

If your tables are correct but the explanation of the output loses marks, our teachers can practise the final sentence with you in online one-to-one Computer Science tuition.

Questions people ask

Which value in a trace table is the output?

The output is whatever the OUTPUT statement names, taken from that variable's value at the moment the statement runs. It is often the last value in that variable's column, but it is not always the last value in the table, because other variables may keep changing.

Why does the loop value not get added at the end?

A WHILE loop tests its condition before each pass. When the condition becomes false, the loop body is skipped. A value that makes the condition false is never processed inside the loop, so it is not added to the total.

Do I need to write a sentence as well as the table?

When the question says 'explain', yes. The table shows the values; the sentence links them. Say which variable is output, which row it comes from and why the loop ended, for example 'the loop stopped when Number became −2'.

Sources

  1. Cambridge IGCSE Computer Science 0478 syllabus page

Updated:

Your next step

If your trace tables are tidy but the output line keeps going astray, a one-to-one teacher can practise the last step with you until reading the answer off the table becomes routine.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

9,000+ students helped through our service