Topic 8 ยท Programming ยท 8.8

Totalling Explained Simply

Totalling means adding values to a running total. Each new value is added to what the program has already collected.

Invitation

How does a program add several values?

A program may need to add marks, prices, scores or measurements as each value is entered.

Totalling uses a variable that stores the running total. Each new value is added to the total already stored.

Start at zero. Add each value. Keep the new total.
Figure 1
A running total
Total starts at 0
โ†“
Add the next value
โ†บ
Store the new total
Big Idea

The total must remember its previous value

The statement Total <- Total + Value uses the old value of Total, adds the new value, then stores the result back in Total.

This statement is usually placed inside a loop so that several values can be added.

The total grows because the variable keeps its value between iterations.
Figure 2
How the value changes
Old total: 8
+
New value: 5
โ†“
New total: 13
FutureLogic Bridge

Totalling is like keeping a running score

Imagine a scoreboard. When a team scores, the points are not written on a new empty board.

The new points are added to the score already displayed. The scoreboard then shows the updated total.

The total is the scoreboard. Each value adds more points.
Figure 3
The running-score bridge
Score: 6
+ 3
Updated score: 9
+ 2
Updated score: 11
Worked Example

Total three numbers

The values 4, 7 and 3 are added to a running total. The total is initialised before the loop begins.

PseudocodeTotalling routine
Total <- 0
FOR Count <- 1 TO 3
    INPUT Number
    Total <- Total + Number
NEXT Count
OUTPUT Total
PythonEquivalent idea
total = 0
for count in range(3):
    number = int(input("Enter a number: "))
    total = total + number
print(total)

Follow the total

0 + 4Total becomes 4
4 + 7Total becomes 11
11 + 3Total becomes 14
Final output: 14
Exam Tip

Initialise the total before the loop

A complete totalling routine usually needs three clear parts: initialise the total, update it inside the loop, then output it after the loop.

Strong pattern:
Total <- 0
Total <- Total + Value
OUTPUT Total

Using the exact variable names given in the question also helps keep the algorithm accurate.

Common Mistake

Forgetting to start the total at zero

If the total is not initialised, the program may try to add a value to an unknown value.

Incorrect: Starting the loop before assigning a value to Total.
Correct: โ€œSet Total to 0 before the loop, then add each value inside the loop.โ€
Summary

The running-total pattern

InitialiseSet the total to zero before the loop.
InputRead the next value.
AddUse Total <- Total + Value.
RepeatContinue for every required value.
StoreThe variable keeps the updated total.
OutputDisplay the final total after the loop.