← IGCSE Computer Science Paper 2

Error: variable not set to zero first

Set a total or counter to 0 before the loop. If you forget, it starts from an unknown value; if you put it inside the loop, it resets every time.

Understanding Error: variable not set to zero first in IGCSE Computer Science Paper 2

What it does: Total ← 0 comes before the FOR loop, so it is set once and then adds up each Num. Count ← 0 is outside the loop, so Count keeps growing instead of being reset on every repeat. Without Total ← 0, the variable begins with an unknown value, and every mark is added to that. The result cannot be trusted. Pseudocode uses ← for assignment. Count ← 0 sets the counter before the loop. The = symbol is used for comparison. Total ← 0 inside the loop wipes out the running total every time. It must be moved above the FOR line.

Common mistakes: Total ← Total does not set the total to 0. It copies an unknown starting value, so every later sum is wrong. It must be Total ← 0 before the loop. Count is used on the right-hand side before it has been given a value, so it starts from an unknown value. Line 3 must be Count ← 0 to set the counter before the loop.

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 232–241) and word search (Modules 232–253).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A program adds up 10 marks in a FOR loop but never sets Total to 0 first. What is the problem?
Total will always end as 0 because it was never setTotal starts from an unknown value, so the final answer may be wrong
Matches
FOR Index ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Index
↓
Total is never set to 0 before the loop, so it starts from an unknown value. Fix: add Total ← 0 before the FOR line.
Applying
Meanings
Term
Total ← 0
Meaning
Set a total or counter to 0 before the loop. If you forget, it starts from an unknown value; if you put it inside the loop, it resets every time.
Practice
DECLARE Total : INTEGER
Testing
Gaps
Total ←
Check Answer
Spots
1Total ← Total
2INPUT Price

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

The 'Wolverine' badge — Fearless Coder, 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 program adds up 10 marks in a FOR loop but never sets Total to 0 first. What is the problem?
A. Total starts from an unknown value, so the final answer may be wrong ✓
B. Total will always end as 0 because it was never set
Without Total ← 0, the variable begins with an unknown value, and every mark is added to that. The result cannot be trusted.
2. Which line correctly prepares a variable to count how many students passed?
A. Count = 0
B. Count ← 0 ✓
Pseudocode uses ← for assignment. Count ← 0 sets the counter before the loop. The = symbol is used for comparison.
3. Where should Total ← 0 be placed in a program that adds numbers in a loop?
A. Inside the loop, on the first line of the body
B. Before the loop starts ✓
Initialise before the loop so it happens once. Inside the loop it would run on every repeat.
4. FOR Index ← 1 TO 5 / Total ← 0 / Total ← Total + Number / NEXT Index. What happens?
A. Total is reset to 0 each repeat, so it only holds the last number ✓
B. Total correctly holds the sum of all five numbers
Total ← 0 inside the loop wipes out the running total every time. It must be moved above the FOR line.
5. A program counts how many scores are above 50, but Count ← 0 is missing before the loop. Which statement is true?
A. Count may start at an unknown value, so the final count could be wrong ✓
B. Count will start at 1 automatically, so only a small error occurs
A counter has no automatic starting value. It must be set with Count ← 0 before the loop.

Match the Pairs

Total ← 0 → Sets the total to 0 before the loop so it starts from a known value
Count ← 0 → Sets the counter to 0 before the loop so it counts from zero
Correct: set the total to 0 once, before the loop starts → Total ← 0 FOR Index ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Index
Correct: set the counter to 0 before the loop so it is not reset each time → Count ← 0 WHILE Count < 10 DO Count ← Count + 1 ENDWHILE
FOR Index ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Index → Total is never set to 0 before the loop, so it starts from an unknown value. Fix: add Total ← 0 before the FOR line.
FOR Index ← 1 TO 5 Count ← 0 Count ← Count + 1 NEXT Index → Count ← 0 is inside the loop, so it resets every time and always ends as 1. Fix: move Count ← 0 above the loop.

Key Term

Total ← 0
Set a total or counter to 0 before the loop. If you forget, it starts from an unknown value; if you put it inside the loop, it resets every time.

Fill in the Blanks

1. Total ← 0 FOR Index ← 1 TO 5
Hint: Type the starting value a total needs before the loop begins
2. Count ← 0 WHILE Num <> 0 DO Count ← Count + 1
Hint: Type the counter variable that must be set to 0 before the loop
3. Total ← 0 FOR Index ← 1 TO 5 Total ← Total + Num
Hint: Type the keyword that starts the loop, which comes after the total is set to 0
4. Count ← 0 REPEAT Count ← Count + 1
Hint: Type the value a counter must hold before it starts counting
5. Total ← 0 WHILE Total < 20 DO Total ← Total + 5
Hint: Type the pseudocode symbol used to assign a value to a variable

Spot the Error

Question 1
A Total ← Total
B INPUT Price
C WHILE Price <> -1 DO
D Total ← Total + Price
E INPUT Price
F ENDWHILE
Line A — Total ← Total does not set the total to 0. It copies an unknown starting value, so every later sum is wrong. It must be Total ← 0 before the loop.
Question 2
A INPUT Limit
B DECLARE Count : INTEGER
C Count ← Count + 1
D WHILE Count < Limit DO
E Count ← Count + 1
F ENDWHILE
Line C — Count is used on the right-hand side before it has been given a value, so it starts from an unknown value. Line 3 must be Count ← 0 to set the counter before the loop.
Question 3
A Total ← 0
B DECLARE Count : INTEGER
C WHILE Total < 100 DO
D Total ← Total + 20
E Count ← Count + 1
F ENDWHILE
Line B — DECLARE only creates the variable. It does not give Count a value, so it starts from an unknown value. It must be Count ← 0 before the loop.
Question 4
A Total ← 0
B FOR Index ← 1 TO 5
C INPUT Mark
D Total ← 0 + Mark
E NEXT Index
F OUTPUT Total
Line D — Using 0 inside the loop resets the total on every repeat, so it only holds the last mark. The loop must add to the running total: Total ← Total + Mark, with Total ← 0 once before the loop.
Question 5
A Count ← 0
B REPEAT
C Count ← 0 + 1
D INPUT Number
E UNTIL Number = 0
F OUTPUT Count
Line C — Resetting the counter inside the loop means Count is always 1, however many numbers are entered. The loop line must be Count ← Count + 1. The 0 belongs before the loop.

Student with a class code? Join your class →