Cambridge IGCSE · Topic 7 (7-9)
IGCSE Computer Science 0478 / Topic 7 / IGCSE

Be the computer.

A dry run means working through an algorithm by hand, one line at a time, exactly as the computer would. A trace table records every change to every variable and every output. It is the most reliable way to find out what an algorithm does, and it is in almost every exam.

The key idea

A dry run is working through an algorithm by hand with given test data. A trace table has a column for each variable and one for OUTPUT (including user prompts). Work line by line: write a new value in the column only when it changes, move down a row as the algorithm moves on, and follow every condition and loop exactly as written, not as you think the programmer meant.

Trace table for the digit-sum algorithm, input 352 Number Digit Sum OUTPUT 352 0 35 2 2 3 5 7 0 3 10 10 one new row each time round the loop; the highlighted row is the third pass
Fig. 1: a completed trace table for the digit-sum algorithm with input 352
Section 01

Filling in a trace table.

Load a program in the lab, enter the test data and step through it line by line. The trace table fills in as each line runs: compare it with the one below.

Count ← 0
Total ← 0
OUTPUT "Enter a number, -1 to stop"
INPUT Number
WHILE Number <> -1 DO
    IF MOD(Number, 2) = 0
      THEN
        Total ← Total + Number
        Count ← Count + 1
    ENDIF
    INPUT Number
ENDWHILE
OUTPUT Count, " even numbers, total ", Total

Test data: 4, 7, 10, 3, -1

NumberCountTotalOUTPUT
00Enter a number, -1 to stop
414
7
10214
3
-12 even numbers, total 14
Examiner trap

The rogue value -1 is input and recorded in the trace table, but the loop ends before it is processed. Do not add it to the total.

Section 02

Loops and selection.

With a FOR loop, the loop counter gets its own column. With an IF, check the condition with the current values every time round: the branch taken can change from one pass to the next.

INPUT Number
FOR Count ← 1 TO 4
    IF MOD(Number, 2) = 0
      THEN
        Number ← DIV(Number, 2)
      ELSE
        Number ← 3 * Number + 1
    ENDIF
    OUTPUT Number
NEXT Count
Worked example Trace with input 6
Complete the trace for the algorithm above when the input is 6.
  1. Number = 6. Count = 1: 6 is even, so Number = DIV(6, 2) = 3. Output 3.
  2. Count = 2: 3 is odd, so Number = 3 × 3 + 1 = 10. Output 10.
  3. Count = 3: 10 is even, so Number = 5. Output 5.
  4. Count = 4: 5 is odd, so Number = 3 × 5 + 1 = 16. Output 16. The loop ends after Count = 4.
Answer: Outputs 3, 10, 5, 16
Your turn
Dry-run the same algorithm with the input 7. What are the four outputs?
Examiner trap

Use the new value of a variable on the next line. After Number becomes 3, the OUTPUT on the same pass shows 3, not 6.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

What is a dry run?

Quick check+10 XP

What does a trace table record?

Quick check+10 XP

In the even-number algorithm, which input ends the loop?

Quick check+10 XP

In the lab, what does DIV(352, 10) evaluate to?

Section 03

Post-condition loops and DIV/MOD.

This algorithm adds up the digits of a whole number. MOD(Number, 10) gives the last digit and DIV(Number, 10) removes it. Fig. 1 shows its trace table with input 352.

OUTPUT "Enter a whole number"
INPUT Number
Sum ← 0
REPEAT
    Digit ← MOD(Number, 10)
    Sum ← Sum + Digit
    Number ← DIV(Number, 10)
UNTIL Number = 0
OUTPUT Sum
Worked example Input 4071
Dry-run the digit-sum algorithm with the input 4071.
  1. Pass 1: Digit = MOD(4071, 10) = 1; Sum = 1; Number = DIV(4071, 10) = 407.
  2. Pass 2: Digit = 7; Sum = 8; Number = 40.
  3. Pass 3: Digit = 0; Sum = 8; Number = 4.
  4. Pass 4: Digit = 4; Sum = 12; Number = 0, so UNTIL Number = 0 is true and the loop stops.
Answer: Output 12
Your turn
What is output for the input 9005, and how many times does the loop run?
Examiner trap

A REPEAT loop always runs at least once, because the condition is tested at the end. With input 0 this algorithm still runs once and outputs 0.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

X ← 2, then a loop runs X ← X * 3 three times. What is the final value of X?

Exam style+20 XP

What is MOD(47, 5)?

Exam style+20 XP

A WHILE loop condition is false the first time it is tested. How many times does the loop body run?

Exam style+20 XP

In the even-number algorithm, the inputs are 2, 8, 5, -1. What is output?

Skill unlocked

Trace tables, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Dry-running algorithms with loops, selection, DIV and MOD, and recording variables and outputs in trace tables.