Loops: counting and totalling

About this lesson: Loops: counting and totalling

In short

A for loop repeats its indented lines once for each value. range(1, 6) gives 1, 2, 3, 4, 5: it stops BEFORE the end number. Change n and watch the loop run.

Loops: counting and totalling

What you'll explore

Count-controlled loops: Repeat a set number of times
A for loop repeats its indented lines once for each value. range(1, 6) gives 1, 2, 3, 4, 5: it stops BEFORE the end number.
Totalling: A running total
To add up a list, start a total at 0, then add each item inside the loop. Watch the trace table: total grows by one mark each time round.
Counting: Count only what matches
A counter also starts at 0, but only goes up by 1 when a condition is True. Count how many marks reach the pass mark, then change the pass mark.
Condition-controlled loops: Repeat until something happens
A while loop keeps going as long as its condition is True. Ava saves the same amount each week until she has at least $100. How many weeks?
Putting it together: Average and highest
One loop can do several jobs: keep a total for the average and remember the highest value so far.
Check yourself: Count the repeats
for i in range(2, 8): print(i)

Also in this lesson

  • Loops to read
  • Watch out for these
  • Choosing the right loop
  • Savings plan
  • More than the lesson
  • Stretch your brain
  • Loop it

Words to know

iteration
Repeating a set of instructions.
loop
Code that repeats.
count-controlled loop
Repeats a set number of times (FOR).
condition-controlled loop
Repeats until a condition changes (WHILE, REPEAT).
range()
Python function giving a sequence of numbers; stops before the end value.
running total
A total that grows as each value is added.
counter
A variable that goes up by 1 each time something happens.
initialise
Give a variable its starting value.
infinite loop
A loop that never stops.
pre-condition / post-condition
Tested before (WHILE) or after (REPEAT) the loop body.

Sources

  • Python documentation: for statements; range()
  • BBC Bitesize, GCSE Computer Science: Iteration
  • Isaac Computer Science: Loops and standard algorithms
  • Computer History Museum: Ada Lovelace's Note G

An interactive lesson with a live experiment: move the sliders, press the buttons and answer quick questions. 6 short steps and a 8-question quiz. Part of the Family plan. Written and checked by Shalo Lab. Last updated 10 November 2026.

← From a problem to an algorithm · Tracing a program →

All lessons in Term 1 · Computing · Age 14