← IGCSE Computer Science Paper 2

Total, count, maximum, minimum and average in one loop

One loop visits each item once, updating the total, count, maximum and minimum together. The average is worked out after the loop ends, as Total / Count.

Understanding Total, count, maximum, minimum and average in one loop in IGCSE Computer Science Paper 2

What it does: Each pass adds the current item to Total and adds 1 to Count, so both are updated together in the same loop. Updates go inside the loop and run for every item. Setting the variables to 0 is initialisation, which belongs before the loop. Starting with the first item means Max and Min begin as real values from the data. Starting Max at 100 and Min at 0 means they can never be replaced by a mark in range. Max is the largest value seen (75) and Min is the smallest (40). The last item is not automatically the maximum or minimum.

Common mistakes: Count must be initialised to 0 before the loop. Starting at 1 makes the final count one too high, so it should be Count ← 0. A counter goes up by 1 for each item. Adding Score[i] turns it into a second total. It should be Count ← Count + 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 286–295) 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
Where should Total, Count, Max and Min be set up in a combined algorithm?
Inside the loop, at the top of each passBefore the loop starts
Matches
FOR i ← 1 TO 5 Total ← 0 Total ← Total + Mark[i] NEXT i
↓
Total is set to 0 inside the loop, so it is reset on every pass and ends up holding only the last item. It must be initialised before the loop.
Applying
Meanings
Term
single pass
Meaning
One loop visits each item once, updating the total, count, maximum and minimum together. The average is worked out after the loop ends, as Total / Count.
Practice
Total ← 0
Testing
Gaps
Count ←
Check Answer
Spots
1Total ← 0
2Count ← 1

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

The 'Moose' badge — Tundra Champion, 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, Count, Max and Min be set up in a combined algorithm?
A. Inside the loop, at the top of each pass
B. Before the loop starts ✓
Initialise before the loop. If Total ← 0 were inside the loop, it would reset on every pass and the running total would be lost.
2. Which is the correct place to calculate the average?
A. After the loop: Average ← Total / Count ✓
B. Inside the loop: Average ← Total / Count on every pass
The average needs the final Total and Count, so it is calculated once the loop has ended. Doing it inside is wasteful and could divide by a Count that is still 0 on the first pass.
3. Which pair of statements belongs INSIDE the loop for each item?
A. Total ← 0 and Count ← 0
B. Total ← Total + Mark and Count ← Count + 1 ✓
Updates go inside the loop and run for every item. Setting the variables to 0 is initialisation, which belongs before the loop.
4. Which is a sensible way to start Max and Min for marks from 0 to 100 stored in an array?
A. Max ← Marks[1] and Min ← Marks[1] ✓
B. Max ← 100 and Min ← 0
Starting with the first item means Max and Min begin as real values from the data. Starting Max at 100 and Min at 0 means they can never be replaced by a mark in range.
5. After a single pass over the marks 40, 75, 60, what are Total and Count?
A. Total = 175, Count = 3 ✓
B. Total = 75, Count = 3
Total adds every mark: 40 + 75 + 60 = 175. 75 is the maximum, not the total. Count goes up by 1 for each of the 3 marks.

Match the Pairs

Total ← 0 Count ← 0 Max ← Mark[1] Min ← Mark[1] → Initialise every variable before the loop starts, with Max and Min set to the first item
Average ← Total / Count → Calculate the average after the loop has finished, using the final total and count
Inside the one loop, the total and the counter are both updated for every item → Total ← Total + Mark[i] Count ← Count + 1
Inside the same loop, maximum and minimum are updated using IF statements that compare each item → IF Mark[i] > Max THEN Max ← Mark[i] IF Mark[i] < Min THEN Min ← Mark[i]
FOR i ← 1 TO 5 Total ← 0 Total ← Total + Mark[i] NEXT i → Total is set to 0 inside the loop, so it is reset on every pass and ends up holding only the last item. It must be initialised before the loop.
FOR i ← 1 TO 5 Total ← Total + Mark[i] Average ← Total / 5 NEXT i OUTPUT Average → The average is calculated inside the loop, before all the items have been added, so it is worked out too early. It should be calculated once after NEXT i.

Key Term

single pass
One loop visits each item once, updating the total, count, maximum and minimum together. The average is worked out after the loop ends, as Total / Count.

Fill in the Blanks

1. Total ← 0 Count ← 0 Max ← Marks[1]
Hint: Type the starting value of the counter before the loop begins.
2. FOR i ← 1 TO 10 Total ← Total + Marks[i] Count ← Count + 1
Hint: Type the name of the array whose element at the current position is added to the total.
3. IF Marks[i] > Max THEN Max ← Marks[i] ENDIF
Hint: Type the comparison operator that checks whether the current mark beats the stored maximum.
4. IF Marks[i] < Min THEN Min ← Marks[i] ENDIF
Hint: Type the keyword that comes after the condition and introduces the action to take.
5. NEXT i Average ← Total / Count OUTPUT Average
Hint: Type the arithmetic operator that divides the total by the count after the loop has ended.

Spot the Error

Question 1
A Total ← 0
B Count ← 1
C Max ← Mark[1]
D Min ← Mark[1]
E FOR i ← 1 TO 10
F Total ← Total + Mark[i]
Line B — Count must be initialised to 0 before the loop. Starting at 1 makes the final count one too high, so it should be Count ← 0.
Question 2
A FOR i ← 1 TO 5
B Total ← Total + Score[i]
C Count ← Count + Score[i]
D IF Score[i] > Max THEN
E Max ← Score[i]
F ENDIF
Line C — A counter goes up by 1 for each item. Adding Score[i] turns it into a second total. It should be Count ← Count + 1.
Question 3
A FOR i ← 1 TO 8
B Total ← Total + Mark[i]
C IF Mark[i] < Min THEN
D Max ← Mark[i]
E ENDIF
F NEXT i
Line D — The IF tests for a new minimum, so the new smallest value must be stored in Min. It should be Min ← Mark[i], not Max.
Question 4
A Min ← 0
B FOR i ← 2 TO 10
C IF Mark[i] < Min THEN
D Min ← Mark[i]
E ENDIF
F NEXT i
Line A — Min must start as the first item (or a very large number). If it starts at 0, no mark is ever smaller, so Min never changes. It should be Min ← Mark[1].
Question 5
A FOR i ← 1 TO 10
B Total ← Total + Mark[i]
C Count ← Count + 1
D NEXT j
E Average ← Total / Count
F OUTPUT Average
Line D — The loop variable is i, so the loop must end with NEXT i. The average is then worked out after the loop, as on the next line.

Student with a class code? Join your class →