Anand Sir · Learn with understanding

How to complete an algorithm trace table

Trace a loop and selection step by step. Practise with original pseudocode, a completed trace table, common mistakes and a second question with a solution.

A trace table records how an algorithm's variables change as each instruction runs. Work in execution order, use the latest stored values and record output only when an OUTPUT instruction executes.

This original practice guide supports algorithm reasoning in school Computer Science. It is not a past-paper question or an official mark scheme. Pseudocode conventions and table layouts vary by course: follow the format supplied in your question.

Example: add only values greater than 3

The input values, in order, are 4, 1, 6 and 3. Predict the final output before reading the table. The arrow means assignment; FOR runs four times, including both endpoints.

Total ← 0
FOR Index ← 1 TO 4
    INPUT Value
    IF Value > 3 THEN
        Total ← Total + Value
    ENDIF
NEXT Index
OUTPUT Total

Completed trace table

This teaching table records the state after each loop iteration. Total starts at 0 before the loop; it changes only when the condition is true.

Values at the end of each iteration
IndexInput ValueValue > 3?Total
14True4
21False4
36True10
43False10

The output is 10. On the second iteration, the condition is false, so Total stays 4. On the third, add 6 to the current total, giving 10. The final input is exactly 3, so it fails the strict greater-than test. OUTPUT runs once, after the loop.

How to trace without guessing

  1. Record initial values before running the loop.
  2. Read the next input only when INPUT executes.
  3. Evaluate the condition using the current values.
  4. Update only the variables changed by the executed instructions.
  5. Check whether the loop repeats before moving to the instruction after it.

Three mistakes that change the answer

  • Resetting Total: initialisation is outside the loop. Moving it inside would discard earlier additions.
  • Including the boundary: Value > 3 excludes 3; Value ≥ 3 would include it and produce 13.
  • Inventing extra outputs: a table of variable values is not the output stream. Here the intermediate totals are never printed.

Practice: trace a running maximum

Use inputs −8, −3 and −12 in that order. Write Largest after each input and the final output.

INPUT Largest
FOR Index ← 1 TO 2
    INPUT Value
    IF Value > Largest THEN
        Largest ← Value
    ENDIF
NEXT Index
OUTPUT Largest
Show the solution and explanation

The first input sets Largest to −8. The next input, −3, is greater than −8, so Largest becomes −3. The final input, −12, is not greater than −3, so Largest remains −3. The output is −3.

Initialising Largest to 0 instead would fail for an all-negative list: 0 is not even one of the inputs. Initialising from the first input avoids that error for a non-empty input sequence.

Apply the method to your course

For Cambridge IGCSE, check the official Computer Science 0478 syllabus and resources for your examination year. IB students should use the notation and course requirements supplied by their school.

If you can trace the values but find it hard to explain them, bring your table and the first uncertain step to an IB Computer Science SL lesson or explore Cambridge Computer Science tutoring. You can check lesson pricing before enquiring.