lesson
Track a result across a collection
1 · Learn
An accumulator stores a running result while a loop visits items. To sum [4, 7, 2], initialise total to zero, then add each item: totals become 4, 11 and 13. Initialising inside the loop would repeatedly discard earlier work.
State how an empty collection is handled. A sum can sensibly be zero, but calculating a mean also requires a non-zero count to avoid division by zero.
2 · Worked example
For [5, 1, 6], running totals are 5, 6 and 12; the mean is 12/3 = 4.
3 · Your turn
What are the running totals for [2, 8, 3], starting from zero?
Check your answer
2, 10 and 13.
4 · Apply your learning
Trace both a correct accumulator and one incorrectly reset inside the loop, explaining the final difference.
Adult guidance and safety
Use an approved sandbox with invented data. Never ask for real passwords, personal records or access to another person's system. These text examples need executable projects and assessed evidence in two languages; reading code alone is not practical mastery.
Practice this lesson
Lesson resources
Browse the available practice activities and worksheets for this lesson.