← IGCSE Computer Science Paper 2

Tracing a totalling algorithm

In a trace table, this line makes the Total column grow by the new Number on every pass. Total must start at 0 before the loop, and this line must sit inside the loop.

Understanding Tracing a totalling algorithm in IGCSE Computer Science Paper 2

What it does: The addition sits inside the loop, so the Total changes once per pass and gives a new value in each trace table row. Total ← Total + Number gives 0+4=4, then 4+6=10, then 10+2=12. The Total column is not the same as the Number column. A variable that has never been set cannot be relied on. Always set Total ← 0 before the loop. Adding outside the loop means only the last Number is added. Move the line inside the loop, before NEXT.

Common mistakes: Total must start at 0. Starting at 1 makes every row of the trace table (and the final output) 1 too big. The line should be Total ← 0. The assignment arrow points to the variable being given a value. The variable goes on the left, so it should be Total ← 0, which sets the first Total value in the trace table to 0.

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 275–285) and word search (Modules 275–295).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A trace table for a totalling algorithm has a Total column. What should the Total column show after each pass of the loop?
The running total so far, which has grown by the latest valueOnly the latest value that was read in
Matches
FOR Count ← 1 TO 3 INPUT Number NEXT Count Total ← Total + Number
↓
The total is added after the loop, so the Total column only changes once at the end and does not grow on each pass. The line should be inside the loop.
Applying
Meanings
Term
Total ← Total + Number
Meaning
In a trace table, this line makes the Total column grow by the new Number on every pass. Total must start at 0 before the loop, and this line must sit inside the loop.
Practice
Total ← 0
Testing
Gaps
Total ←
Check Answer
Spots
1DECLARE Total : INTEGER
2Total ← 1

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

The 'Deer' badge — Forest Streak, 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 trace table for a totalling algorithm has a Total column. What should the Total column show after each pass of the loop?
A. Only the latest value that was read in
B. The running total so far, which has grown by the latest value ✓
The Total column records the running total. Each pass adds the new Number, so the total keeps growing.
2. Numbers input are 4, 6 and 2. Total starts at 0. Which Total column is correct after each pass?
A. 4, 10, 12 ✓
B. 4, 6, 2
Total ← Total + Number gives 0+4=4, then 4+6=10, then 10+2=12. The Total column is not the same as the Number column.
3. Which line must come BEFORE the loop so the first row of the trace table is right?
A. Total ← 0 ✓
B. Total ← Total + Number
Total must be set to 0 before the loop. Otherwise the first pass adds to an unknown starting value.
4. A student forgets Total ← 0 and the algorithm still runs. What does the Total column show on the first row of the trace table?
A. The first value is added to a starting value that was never set, so the total is wrong ✓
B. The total is always right because it starts at 0 by magic
A variable that has never been set cannot be relied on. Always set Total ← 0 before the loop.
5. Where should Total ← Total + Number be placed in a totalling algorithm?
A. After the loop, so it runs once at the end
B. Inside the loop, so it runs once for every value ✓
If the line is after the loop, it runs only once and adds only the last value. Inside the loop, it runs every pass.

Match the Pairs

Total ← Total + Number → On each pass, the Total column shows the old total plus the new Number, so it grows row by row in the trace table
Total ← 0 → Sets the Total column to 0 in the first row of the trace table, before any values are added
For the values 4, 6 and 5, the Total column reads 0, 4, 10, 15 because the total grows after each value is added → FOR Count ← 1 TO 3 INPUT Number Total ← Total + Number NEXT Count
The total must be set to 0 before the loop starts so the first row of the trace table has a correct starting value → Total ← 0 FOR Count ← 1 TO 5 Total ← Total + Number[Count] NEXT Count
FOR Count ← 1 TO 3 INPUT Number NEXT Count Total ← Total + Number → The total is added after the loop, so the Total column only changes once at the end and does not grow on each pass. The line should be inside the loop.
FOR Count ← 1 TO 3 INPUT Number Total ← Total + Number NEXT Count → Total was never set to 0 before the loop, so the first row of the trace table has no valid starting value and the totals will be wrong.

Key Term

Total ← Total + Number
In a trace table, this line makes the Total column grow by the new Number on every pass. Total must start at 0 before the loop, and this line must sit inside the loop.

Fill in the Blanks

1. Total ← 0 FOR Count ← 1 TO 4 INPUT Number
Hint: Type the starting value Total needs before the loop, so the first row of the trace table is correct.
2. FOR Count ← 1 TO 4 Total ← Total + Number NEXT Count
Hint: Type the arithmetic operator that makes the Total column grow by each new Number.
3. FOR Count ← 1 TO 4 Total ← Total + Number NEXT Count
Hint: Type the variable that holds the running total, so the old total is kept and added to.
4. FOR Count ← 1 TO 4 Total ← Total + Number NEXT Count
Hint: Type the keyword that ends the loop, so the adding line is repeated on every pass.
5. NEXT Count OUTPUT Total
Hint: Type the variable whose final value is the last row of the trace table, to be output after the loop.

Spot the Error

Question 1
A DECLARE Total : INTEGER
B Total ← 1
C FOR Count ← 1 TO 3
D Total ← Total + Count
E NEXT Count
F OUTPUT Total
Line B — Total must start at 0. Starting at 1 makes every row of the trace table (and the final output) 1 too big. The line should be Total ← 0.
Question 2
A 0 ← Total
B FOR Count ← 1 TO 4
C INPUT Number
D Total ← Total + Number
E NEXT Count
F OUTPUT Total
Line A — The assignment arrow points to the variable being given a value. The variable goes on the left, so it should be Total ← 0, which sets the first Total value in the trace table to 0.
Question 3
A Total ← Number
B REPEAT
C INPUT Number
D Total ← Total + Number
E UNTIL Number = 0
F OUTPUT Total
Line A — Number has no value before the loop, so Total starts with nothing useful in the trace table. Total must be set to 0 before the loop: Total ← 0.
Question 4
A Total ← 0
B FOR Day ← 1 TO 7
C INPUT Steps
D Total ← Steps
E NEXT Day
Line D — This replaces Total with the latest Steps value instead of adding to it. The Total column would not grow. It should be Total ← Total + Steps.
Question 5
A DECLARE Total : INTEGER
B DECLARE Number : INTEGER
C Count ← 0
D FOR Count ← 1 TO 3
E INPUT Number
F Total ← Total + Number
Line C — This sets the wrong variable to 0. Total is never given a starting value, so the first trace table row is wrong. The line should be Total ← 0.

Student with a class code? Join your class →