← IGCSE Computer Science Paper 2

Dry-running an algorithm and giving its output

Following an algorithm line by line with given data, recording each variable's value in a trace table, to find its output or purpose.

Understanding Dry-running an algorithm and giving its output in IGCSE Computer Science Paper 2

What it does: This line changes Total on every loop. You write the new value of Total in the trace table each time, using the given data. Assignment replaces the old value with the result of the right-hand side: 10 + 5 = 15. Nothing is output unless the line says OUTPUT. The loop runs for i = 1, 2, 3 and 4, which is 4 times. Count goes 0, 1, 2, 3, 4, so the final value is 4. A and B are numbers, so A + B adds them: 6 + 9 = 15. Joining digits as text (69) would not happen here.

Common mistakes: A FOR loop in Cambridge pseudocode is closed with NEXT i, not ENDFOR. Without NEXT i you cannot trace the loop, but with it the dry run gives Total = 1, 3, 6 and OUTPUT 6. N must go down so the condition N > 0 eventually becomes false. With N + 1 the trace table shows 3, 4, 5... forever. It should be N ← N - 1, giving OUTPUT 3, 2, 1.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 307–316) and word search (Modules 296–316).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A dry run of an algorithm is carried out using...
A trace table and given test data, line by lineA compiler that runs the program automatically
Matches
Trace table row for Count: 0, 1, 2, 3 when the loop only ran twice
↓
The trace has an extra row, so the loop was traced one more time than it really runs. Check the loop condition against the data.
Applying
Meanings
Term
dry run
Meaning
Following an algorithm line by line with given data, recording each variable's value in a trace table, to find its output or purpose.
Practice
Total ← 0
Testing
Gaps
OUTPUT X // trace shows
Check Answer
Spots
3 Total ← Total + i
4ENDFOR i

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Kangaroo Rat' badge — Speed Typer, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A dry run of an algorithm is carried out using...
A. A trace table and given test data, line by line ✓
B. A compiler that runs the program automatically
A dry run is done by hand. You follow each line with the given data and record every variable's value in a trace table.
2. Which row of a trace table is created by the line Total ← Total + 5 when Total was 10 before the line?
A. Total stays at 10 and 5 is output
B. Total changes to 15 ✓
Assignment replaces the old value with the result of the right-hand side: 10 + 5 = 15. Nothing is output unless the line says OUTPUT.
3. Dry run: Count ← 0, then FOR i ← 1 TO 4 loops with Count ← Count + 1. What is the final value of Count?
A. 4 ✓
B. 5
The loop runs for i = 1, 2, 3 and 4, which is 4 times. Count goes 0, 1, 2, 3, 4, so the final value is 4.
4. Dry run with input 6 and 9: INPUT A, INPUT B, OUTPUT A + B. What is the OUTPUT?
A. 69
B. 15 ✓
A and B are numbers, so A + B adds them: 6 + 9 = 15. Joining digits as text (69) would not happen here.
5. When you dry run an algorithm, a new row in the trace table should be written...
A. Whenever a variable's value changes or something is output ✓
B. Only once at the very end of the algorithm
A trace table records the values step by step, so you can see where an error or the final OUTPUT comes from.

Match the Pairs

dry run → Following an algorithm line by line with given data, recording each variable's value, to find its output or purpose
Total | Count | OUTPUT → The column headings of a trace table: one column for each variable and one for each OUTPUT
Tracing with given input values: the data 4, 7 and 9 is entered and the total is updated each time round the loop → Total ← Total + Number
Stating the final OUTPUT: it is written only once the trace reaches the end of the loop, using the last values in the table → OUTPUT Total
Trace table row for Count: 0, 1, 2, 3 when the loop only ran twice → The trace has an extra row, so the loop was traced one more time than it really runs. Check the loop condition against the data.
Dry run that only writes the final OUTPUT and skips the trace table rows → The student did not record each variable's value line by line, so mistakes cannot be found and method marks are lost.

Key Term

dry run
Following an algorithm line by line with given data, recording each variable's value in a trace table, to find its output or purpose.

Fill in the Blanks

1. X ← 2 X ← X * 3 OUTPUT X // trace shows 6
Hint: Dry run the two assignments and type the final value of X that the trace table shows.
2. FOR N ← 1 TO 3 OUTPUT N NEXT N
Hint: Type the keyword that separates the start value from the end value, so the trace table shows N as 1, 2, 3.
3. IF Mark < 50 THEN OUTPUT "Fail" ENDIF
Hint: Type the keyword that closes the selection after you have checked whether the condition is true in the trace.
4. Total ← 10 Total ← Total - 4 OUTPUT Total // trace shows 6
Hint: Use the final value in the trace to work out which arithmetic operator was used in the assignment.
5. Total ← 3 Total ← Total + 2 OUTPUT Total
Hint: Type the keyword that displays the final value shown at the end of a dry run.

Spot the Error

Question 1
A Total ← 0
B FOR i ← 1 TO 3
C Total ← Total + i
D ENDFOR i
E OUTPUT Total
Line D — A FOR loop in Cambridge pseudocode is closed with NEXT i, not ENDFOR. Without NEXT i you cannot trace the loop, but with it the dry run gives Total = 1, 3, 6 and OUTPUT 6.
Question 2
A N ← 3
B WHILE N > 0 DO
C OUTPUT N
D N ← N + 1
E ENDWHILE
Line D — N must go down so the condition N > 0 eventually becomes false. With N + 1 the trace table shows 3, 4, 5... forever. It should be N ← N - 1, giving OUTPUT 3, 2, 1.
Question 3
A Max ← 4
B Num ← 9
C IF Num < Max THEN
D Max ← Num
E ENDIF
F OUTPUT Max
Line C — The test is the wrong way round. The dry run checks 9 < 4, which is false, so Max stays 4 and the output is wrong. It should be Num > Max, so Max becomes 9.
Question 4
A Nums[1] ← 5
B Nums(2) ← 8
C Total ← Nums[1] + Nums[2]
D OUTPUT Total
Line B — Array elements use square brackets in Cambridge pseudocode, so it should be Nums[2] ← 8. A dry run then gives Total = 5 + 8 = 13.
Question 5
A Age ← 16
B IF Age >= 16
C OUTPUT "Adult"
D ENDIF
Line B — The IF line is missing THEN. It should be IF Age >= 16 THEN. When you dry run with Age = 16, the condition is true and the OUTPUT is Adult.

Student with a class code? Join your class →