GCSE · Computer Science · Edexcel · Spec 1CP2

Count-controlled repetition

Need to print a line of stars 50 times? Don't paste it 50 times. Write two lines and let a for loop do the counting.

Computer Science · Iteration

Watch count skip along

range(2, 11, 2) means: start at 2, stop at 11, go up in steps of 2. Press Next to run the loop one pass at a time and watch what count holds.

Before you press Next: how many times will this loop run — and will 11 ever be printed?

1 for count in range(2, 11, 2):
2 print(count)
3 print("Finished!")
Passcount
——

Output

 

Step 1: Nothing has printed yet. First, range sets up the sequence the loop will walk along: 2, 4, 6, 8, 10. It's fixed before the first pass, so the loop already knows it will run 5 times. 11 isn't in the sequence. It's only where the counting stops.

1 / 7

Step through the trace — every value is the lesson’s, not run by the page.

Step 1 of 7: Nothing has printed yet. First, range sets up the sequence the loop will walk along: 2, 4, 6, 8, 10. It's fixed before the first pass, so the loop already knows it will run 5 times. 11 isn't in the sequence. It's only where the counting stops..

Watch out: The middle number is the stop point, not the last value. The last value count takes here is 10, because range always stops before the stop number.

Computer Science · range

What does range(5) really give you?

The loop is: for count in range(5): print(count). There's only one number in the brackets this time.

What will this loop print? Pick the answer closest to what you honestly think.
How sure are you?

Computer Science · range

How many passes?

for count in range(0, 14, 2): — how many times does the indented block run? Commit to a number first.

Your estimate

8 passes

0 passes15 passes

Computer Science · for loops

Let a variable set the count

A program has to say Welcome! once to every user. Copying and pasting print("Welcome!") only works if you know how many users there'll be. Rebuild it so the program asks how many users there are and loops that many times.

  1. num_users = int(input("How many users? "))Ask the question and store the answer in num_users. int() turns what's typed into a whole number, so range can use it.
  2. missing step
Which line is step 2?

Computer Science · Your turn

Write the loop yourself

Write a for loop that uses range with a start, a stop and a step to print 5, 10, 15, 20 (one number per line). Then describe what the count variable does while the loop runs. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

A for loop knows how many times it will run before it starts. It runs once for each value range gives it, then stops by itself.

What you need to know

  • What iteration is, and what makes it count-controlled
  • How a for loop uses range to decide how many times it repeats
  • What range gives you with one, two or three numbers, and why the stop number is never included
  • What the count variable holds on each pass, and when the loop ends
  • How to make the number of repetitions come from a variable

The big picture

Iteration means repeating instructions in a loop, and when a count decides how many repeats there are, it's count-controlled. In Python that's a for loop: for every value in a sequence, it runs the indented block once. range generates the sequence. One number is the stop point (counting from 0), two are start and stop, and three are start, stop and step. The stop number is never included. On each pass the count variable holds the next value, and when the values run out the loop ends by itself. The number of passes can come from a variable, such as num_users.

Key points

1Iteration means repeating a sequence of instructions in a loop. When a count value decides how many times they repeat, it's count-controlled iteration.
2In Python, a for loop does count-controlled iteration: for every value in a sequence, it runs the indented block once.
3range is a built-in function that generates the sequence. One number is the stop point, counting from 0: range(5) gives 0, 1, 2, 3, 4. Two numbers are the start and stop. Three are the start, stop and step: range(0, 14, 2) gives 0, 2, 4, 6, 8, 10, 12.
4The stop number is never in the sequence. It's only where the counting stops.
5The count variable takes the next value in the sequence on each pass. When the sequence runs out, the loop ends automatically.
6The number of repetitions can come from a variable, e.g. for count in range(num_users):, instead of copying and pasting the same line.

Worked example

Problem

A running app should print Lap 1, Lap 2, Lap 3, Lap 4 and Lap 5. A student writes: for lap in range(1, 5): print("Lap", lap) What does it actually print, and how do you fix it?

⚠ Watch out

Reading the stop number as the last value. range(2, 11, 2) ends on 10, not 11, because the stop point is never part of the sequence.

🧠

Memory hook

Start, stop, step, and stop means stop BEFORE.

✓

Check yourself

Without running it, how many times does for count in range(3, 9, 2): run, and what does print(count) output on each pass?

Flashcards

(13)
What is iteration?
Repeating a sequence of instructions inside a loop.
What makes iteration count-controlled?
A count value decides how many times the instructions repeat.
Which Python loop is used for count-controlled iteration?
The for loop. For every value in a sequence, it runs the indented block once.
What does range do?
It's a built-in function that generates a sequence of numbers for a for loop to work through.
In range(start, stop, step), what does each number do?
start is the first value, stop is where counting stops (not included), and step is how much to go up by each time.
What does range(5) generate?
0, 1, 2, 3, 4. It starts at 0 and stops before 5.
Why isn't the stop number in the sequence?
It's used as the stop point, so range stops before it gets there.
What is the count variable?
The variable in the for line, e.g. count. On each pass it holds the next value in the sequence.
When does a for loop end?
Automatically, once the count variable has taken the last value in the sequence.
How many times does for count in range(3, 8): run?
5 times. count takes 3, 4, 5, 6 and 7.
How do you make a loop repeat once for every user?
Store the number of users in a variable, e.g. num_users, and loop over range(num_users).
Why use a loop instead of copying and pasting the same line?
The line is written once, and the number of repeats can change. It can even come from a variable.
Which range gives 10, 20, 30?
range(10, 31, 10). The stop just has to be past 30, but not past 40, or 40 sneaks in.

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 posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

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 29 September 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.