GCSE · Computer Science · Edexcel · Spec 1CP2
Programming – count-controlled repetition
Here's a two-line program: for count in range(0, 14, 2): print(count). Will it ever print 14? Step through it and find out.
Computer Science · Algorithms
Trace table — dry run the code
Press Next and watch count. Which number is printed last, and why isn't it 14?
Ready when you are — step through one line at a time.
Your turn: read a range
Three values this time. Say which is the start, which is the stop and which is the step before you pick.
range(2, 11, 2): which pair gives its first and last values?
Work backwards
This time you know the numbers you want and have to choose the range.
You're printing a times table up to 12 x 5, so the loop variable has to run from 1 to 12. Which call gives exactly that?
Computer Science · Programming
A loop inside a loop
Step through and watch what the inner loop does every time the outer loop moves on.
Before you step: the outer loop repeats 3 times and the inner loop repeats 2 times. How many times will the print line run?
1 for outer in range(3):2 for inner in range(2):3 print(outer, inner)
| outer | inner | inner code runs |
|---|---|---|
| 0 | 0 |
Output
Step 1: The outer loop starts with its first value, 0.
Step through the trace — every value is the lesson’s, not run by the page.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Repeat code a set number of times, and learn why the stop value is the number the loop never reaches.
What you need to know
- Iteration means repeating a sequence of instructions inside a program loop.
- Count-controlled iteration uses a count value to decide how many times the instructions repeat. In Python, that's a for loop.
- A variable like count takes each value in the range in turn. The code inside the loop runs once for each value.
- When the sequence of values runs out, the loop ends by itself.
Have a goYour mate Sam says: 'A for loop that goes through 4 values prints four times, then sits there waiting for someone to stop it.' What do you tell Sam?
Sam's wrong: the loop ends by itself once the sequence of values runs out.
After the last value has been used, the sequence has run out, so the loop finishes without anyone stopping it.
- With one value, range() uses it as the stop and starts at 0: range(5) gives 0, 1, 2, 3, 4.
Have a goBe the computer: range(6) has just one value. What numbers does it give?
0, 1, 2, 3, 4, 5
One value is the stop, so counting starts at 0. Like range(5) giving 0 to 4, the stop itself isn't in the list.
- Two values are start and stop. Three are start, stop and step, so range(0, 10, 2) steps up by 2.
- The stop value is never included. range(0, 14, 2) gives 0, 2, 4, 6, 8, 10 and 12, so 12 is the last number printed.
- A for loop removes repeated code, and the number of repeats can come from a variable, such as num_users.
Have a goIf the number of users changes every time the program runs, why does one loop beat 30 copied input lines?
The repeat count can come from a variable like num_users, so the same loop fits any number of users.
Copied lines fix the number of repeats in advance. A loop can read the count from a variable.
- Nested loops put one loop inside another. The outer loop controls how many times the inner loop is repeated.
- The inner loop runs completely, from its beginning, each time the outer loop runs once. Outer 3, inner 2: 3 x 2 = 6 runs.
The big picture
A for loop repeats code a set number of times, and range() decides which values the loop variable takes. The stop value is never reached, and that's the bit that catches people out. You'll also see how a loop replaces copied code and what a loop inside a loop really does.
Key points
Worked example
Problem
What does this program print, and which number is printed last? for n in range(3, 15, 3): print(n)
⚠ Watch out
Believing the stop value gets used, and that the inner loop runs just once for each outer repetition. The sequence never includes the stop value, and the inner loop runs completely every time the outer loop runs once.
Memory hook
The stop value is the number the loop never reaches.
Check yourself
Think it through: for i in range(2, 20, 5), what is the last value i takes, and does the loop ever reach 20?
Flashcards
(12)What is iteration?
What makes iteration count-controlled, and which Python loop does it?
In for count in range(...), what does count do?
How does a for loop over a range finish?
What does range() do when you give it one value?
What do two values and three values mean in range()?
Is the stop value part of the sequence range() generates?
What kind of code can a for loop replace?
Where can a loop's number of repeats come from?
What are nested loops?
What does the inner loop do each time the outer loop runs once?
An outer loop repeats 6 times and its inner loop repeats 3 times. How many times does the inner code run?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 9 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.