Count-controlled loops: for
range, the loop variable, and repeating a known number of times.
Covers OCR J277 2.2.1 Programming fundamentals · AQA 8525 3.2.2 Programming concepts · Edexcel 1CP2 6.2.2 Sequence, selection, repetition and iteration
Do this lesson in the simulatorPrint it: worksheet (PDF) · answers (PDF) · as a web page
To drive a square you could write forward, turn, forward, turn, forward, turn, forward, turn. Eight lines for one idea repeated four times. A loop writes the idea once and says how many times. Repeating code is called iteration, and a loop that runs a set number of times is count-controlled.
for and range
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
for i in range(4):
print("side", i)
forward(60, distance=25)
turn_right(30, angle=90)
Three things in the first line. range(4) produces four numbers: 0, 1, 2, 3. for i in takes them one at a time and puts each into the loop variable i. The indented block runs once for each. Four sides, four turns, a square.
The block under a for is just like the block under an if: colon, indent, same rules.
Counting from zero
Programmers count from zero, so range(4) is 0, 1, 2, 3 and never 4. If you want to print 1 to 4 for a human, add one, or start the range at 1:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
for i in range(4):
print("corner number", i + 1)
for n in range(1, 5):
print("also", n)
for n in range(0, 20, 5):
print("in fives", n)
range(1, 5) gives 1, 2, 3, 4: it starts at the first number and stops before the second. A third number is the step: range(0, 20, 5) counts 0, 5, 10, 15.
The loop variable is useful
i changes every time round. Use it. Turn your sound on for this one:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
for step in range(8):
note = 300 + step * 50
print("step", step, "note", note)
tone(note, 0.2)
Each time round, the note is worked out from step, so the piezo climbs. Change 50 to 100, or 8 to 12.
Predict, then run
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
total = 0
for i in range(5):
total = total + i
print("i is", i, "total is", total)
print("final total", total)
Work through it by hand first: what is total after each time round? Writing the values down in a table, one row per time round, is exactly what an exam trace table is.
Loops and decisions together
A block can hold an if, so a loop can react each time round:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
for i in range(8):
forward(50, distance=8)
if distance() < 30:
led("red")
print("getting close at step", i)
else:
led("green")
The edge of the mat starts 81 cm ahead, so for the first six steps there is room and the LED stays green. At steps 6 and 7 only 24 and 16 cm are left: the LED turns red and the message prints.
Task: count the corners
Drive a square of 30 cm sides with a for loop, printing corner 1, corner 2, corner 3, corner 4 as you go, and finish facing the way you started.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
for i in range(4):
forward(60, distance=30)
Task: play a scale
Use one for loop to play five notes, each for a quarter of a second: 400, 500, 600, 700 and 800 hertz. Work each note out from the loop variable; do not type the notes after the first.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
for i in range(5):
tone(400, 0.25)
Challenges
- Draw a triangle with a
forloop (three sides,turn_right(30, angle=120)). - Blink the LED five times using
wait(0.2)inside a loop. - Print the 7 times table, from 7 to 70, with one loop.
Past paper questions on this
Real exam questions that test this, each worked through with a program you can run.
- AQA 8525 June 2025 Paper 1, Question 5: Data types, and five inputs rewritten as a loop with one 8 marks
- AQA 8525 June 2023 Paper 1, Question 12.4: Fix a subroutine's parameter and a FOR loop that counts down 3 marks
- AQA 8525 June 2022 Paper 1, Question 1: MOD, selection, assignment, relational operators and a definite loop 6 marks
- AQA 8525 Sample Paper 1, Question 3: The dog biscuit algorithm: selection, iteration, subroutine calls and a data type 5 marks
- OCR J277 June 2025 Paper 2, Question 2(a), (b): Count the passes of a flowchart loop, and describe the kinds of iteration 8 marks
- OCR J277 June 2023 Paper 2, Question 5(c): An adding game: three random questions and a score 6 marks
- OCR J277 June 2022 Paper 2, Question 4(c): Input n numbers, then output the total and the average 6 marks
- OCR J277 Sample Paper 2, Question 8(b) to (d): A range check with OR and its tests, index a 2D array, refine repeated lines into a loop 10 marks