GCSE · Computer Science · Edexcel · Spec 1CP2
Programming – count-controlled repetition
Two lines of code: for count in range(0, 14, 2): print(count). Will 14 ever appear on the screen? Make a guess, then step through it and see.
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
Flip it round: you know the numbers you want, so now 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 keep your eye on inner every time outer 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.
Make code repeat a set number of times, and meet the stop value: the number the loop never reaches.
What you need to know
- Iteration means repeating a sequence of instructions inside a program loop.
- When a count value decides how many times the instructions repeat, that's count-controlled iteration. Python does it with a for loop.
- A variable such as count takes each value in the range in turn, and the code inside runs once per value.
- Once the sequence of values runs out, the loop ends automatically. Nobody has to stop it.
Have a goYour mate Sam is sure: 'A for loop over 4 values prints four times, then just sits there until someone stops it.' What do you tell Sam?
Sam's wrong: the loop prints four times, then ends by itself because the values have run out.
After the last value has been used the sequence has run out, so the loop finishes on its own. Nothing is left for it to wait for.
- Give range() one value and that's the stop, with the sequence starting at 0: range(5) gives 0, 1, 2, 3, 4.
Have a goBe the computer: range(6) has just one value inside the brackets. Which numbers does it give?
0, 1, 2, 3, 4, 5
One value is the stop, so counting starts at 0 and finishes one before 6. Writing 1 to 6 starts in the wrong place and lets the stop in.
- Two values mean start and stop. Three mean 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, 12, so 12 is printed last.
- Copied lines are a cue for a loop, and the number of repeats can come from a variable, such as num_users.
Have a goToday 3 users sign in; tomorrow 30 will. Why does one loop with range(num_users) beat copying the input line for each user?
The loop takes its count from num_users, so it repeats 3 times today and 30 tomorrow without a single change to the code.
Copied lines fix the number of repeats when the code is written. A loop whose count comes from num_users fits whatever number is entered.
- Nested loops put one loop inside another. The outer loop controls how many times the inner loop is repeated.
- Each time the outer loop runs once, the inner loop runs completely from its beginning. Outer 3, inner 2: 3 x 2 = 6.
The big picture
A for loop repeats a block of code a specific number of times, and range() decides which values the loop variable takes. Here's the twist: the stop value never makes it into the sequence. You'll also swap copied code for a loop and find out 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
Expecting the stop value to be printed, and picturing the inner loop taking one step for each outer repeat. The sequence ends before 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 is count doing?
How does a for loop over a range finish?
range() with one value: what does that value mean, and where does the sequence start?
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, apart from a number typed into the code?
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
- Evaluating program fitness and efficiency
- Functions vs procedures
- Global vs local variables
- Identifying and correcting program errors
- Primitive data types
- Programming – condition-controlled repetition
- Programming – iteration over data structures
- Programming – selection
- Programming – sequencing
- Programming arithmetic operators
- Programming logical operators
- Programming relational operators
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 26 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.