← IGCSE Computer Science Paper 2

Totalling in a loop (8.1b)

Adds each new value to the running total inside a loop. Set Total <- 0 before the loop starts, then OUTPUT Total after the loop ends.

Understanding Totalling in a loop (8.1b) in IGCSE Computer Science Paper 2

What it does: The total is set to 0 before the loop. The FOR loop repeats 10 times and each time adds the new Number to Total. Total needs a known starting value of 0. Without it, the first Total + Number uses an unknown value. OUTPUT Total after the loop shows the final total once. Inside the loop it would print many partial totals, or an old one. 0 + 4 = 4, then 4 + 6 = 10, then 10 + 10 = 20. The total keeps building up. It does not just keep the last value. The rogue value only signals the end of input. If -1 is added, the total will be 1 too small.

Common mistakes: Total <- Number replaces the total with the latest value, so earlier values are lost. It should be Total <- Total + Number to add each value to the running total. A FOR loop uses the keyword TO for its end value, not UNTIL. It should be FOR Count <- 1 TO 3.

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 379–388) and word search (Modules 379–398).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Where should Total <- 0 be written when totalling values in a loop?
Before the loop startsInside the loop, as the first line
Matches
FOR Count <- 1 TO 5 Total <- 0 INPUT Number Total <- Total + Number NEXT Count
↓
Total <- 0 is inside the loop, so the total is reset to zero on every repeat and only the last number is kept. It should be placed before the loop.
Applying
Meanings
Term
Total <- Total + Number
Meaning
Adds each new value to the running total inside a loop. Set Total <- 0 before the loop starts, then OUTPUT Total after the loop ends.
Practice
DECLARE Total : INTEGER
Testing
Gaps
Total <-
Check Answer
Spots
3 INPUT Number
4 Total <- Number

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

The 'Angelfish' badge — Reef Perfectionist, 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. Where should Total <- 0 be written when totalling values in a loop?
A. Inside the loop, as the first line
B. Before the loop starts ✓
Total must be set to 0 once, before the loop. If it is inside the loop, it resets to 0 every time round and the running total is lost.
2. Which line adds the new value Number to the running total?
A. Total <- Total + Number ✓
B. Number <- Total + Number
The result must be stored back in Total. Total <- Total + Number takes the old total, adds the new value and saves it in Total.
3. A student forgets to set Total <- 0 before the loop. What is the problem?
A. Total has no starting value, so the answer may be wrong ✓
B. The loop will run one time too many
Total needs a known starting value of 0. Without it, the first Total + Number uses an unknown value.
4. Which FOR loop correctly totals 5 numbers typed in by the user?
A. Total <- 0 / FOR Count <- 1 TO 5 / INPUT Number / Total <- Total + Number / NEXT Count ✓
B. FOR Count <- 1 TO 5 / INPUT Number / Total <- 0 / Total <- Total + Number / NEXT Count
In the wrong version, Total <- 0 is inside the loop, so it resets every time and only the last number is kept.
5. Where should OUTPUT Total be placed to show the final total of all the values?
A. Inside the loop, before the total is added
B. After the loop (after NEXT or ENDWHILE) ✓
OUTPUT Total after the loop shows the final total once. Inside the loop it would print many partial totals, or an old one.

Match the Pairs

Total <- 0 → Sets the running total to zero before the loop starts, so the adding begins from a known value
Total <- Total + Number → Adds the new value to the running total and stores the result back in Total each time round the loop
Totalling ten values typed in by the user: set the total to 0, then add each input inside a FOR loop → Total <- 0 FOR Count <- 1 TO 10 INPUT Number Total <- Total + Number NEXT Count
Totalling until a rogue value is entered, then showing the total after the loop has finished → Total <- 0 INPUT Number WHILE Number <> -1 DO Total <- Total + Number INPUT Number ENDWHILE OUTPUT Total
FOR Count <- 1 TO 5 Total <- 0 INPUT Number Total <- Total + Number NEXT Count → Total <- 0 is inside the loop, so the total is reset to zero on every repeat and only the last number is kept. It should be placed before the loop.
Total <- 0 FOR Count <- 1 TO 5 INPUT Number Total <- Number NEXT Count → Total <- Number replaces the total with the latest value instead of adding to it. It should be Total <- Total + Number.

Key Term

Total <- Total + Number
Adds each new value to the running total inside a loop. Set Total <- 0 before the loop starts, then OUTPUT Total after the loop ends.

Fill in the Blanks

1. Total <- 0 FOR Count <- 1 TO 5 INPUT Number
Hint: Type the starting value the total must have before the loop begins.
2. FOR Count <- 1 TO 5 Total <- Total + Number NEXT Count
Hint: Type the variable holding the new value that is added to the running total.
3. INPUT Number Total <- Total + Number NEXT Count
Hint: Type the variable name that keeps the running total, so the old total is carried forward.
4. WHILE Number <> -1 DO Total <- Total + Number INPUT Number
Hint: Type the keyword that starts a pre-condition loop which stops at a rogue value.
5. NEXT Count OUTPUT "Sum: ", Total
Hint: Type the variable whose final value is displayed after the loop has finished.

Spot the Error

Question 1
A Total <- 0
B FOR Count <- 1 TO 5
C INPUT Number
D Total <- Number
E NEXT Count
F OUTPUT Total
Line D — Total <- Number replaces the total with the latest value, so earlier values are lost. It should be Total <- Total + Number to add each value to the running total.
Question 2
A Total <- 0
B FOR Count <- 1 UNTIL 3
C INPUT Number
D Total <- Total + Number
E NEXT Count
F OUTPUT Total
Line B — A FOR loop uses the keyword TO for its end value, not UNTIL. It should be FOR Count <- 1 TO 3.
Question 3
A DECLARE Total : INTEGER
B Total <- 1
C FOR Count <- 1 TO 5
D INPUT Number
E Total <- Total + Number
F NEXT Count
Line B — The total must start at 0 before the loop. Starting at 1 makes every final total one too big. It should be Total <- 0.
Question 4
A Total <- 0
B INPUT Number
C WHILE Number <> -1 DO
D Total + Number <- Total
E INPUT Number
F ENDWHILE
Line D — The assignment is the wrong way round. The variable being updated goes on the left of <-. It should be Total <- Total + Number.
Question 5
A Total <- 0
B WHILE
C INPUT Number
D Total <- Total + Number
E UNTIL Total >= 100
F OUTPUT Total
Line B — A loop that ends with UNTIL must begin with REPEAT, not WHILE. Line 2 should be REPEAT.

Student with a class code? Join your class →