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?

1for count in range(0, 14, 2):
2 print(count)
count
Output
Press Start to run the first line.
·

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

Swap the copies for a loop

This program repeats itself. Build the loop that replaces the copies, then make the number of repeats come from a variable.

  1. print(0) print(1) print(2) print(3) print(4)Five print statements to output 0 to 4. Five copies of nearly the same job is the cue to use a loop.
  2. missing step
Which line is step 2?

Exam line: Copied lines are a sign to use a for loop. The count can come from a variable, such as num_users.

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)
outerinnerinner code runs
00

Output

 

Step 1: The outer loop starts with its first value, 0.

1 / 16

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

Step 1 of 16: The outer loop starts with its first value, 0..

Exam line: The inner code runs (outer count) x (inner count) times. Here that is 3 x 2 = 6.
Watch out: Easy trap: picturing the inner loop as one step per outer go. Each time the outer loop runs once, the inner loop runs all the way through.

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

1Count-controlled iteration repeats instructions a set number of times; in Python a for loop implements it.
2range(stop), range(start, stop) and range(start, stop, step): the stop value is never included.
3A for loop replaces repeated code, and its number of repeats can come from a variable such as num_users.
4In nested loops the inner loop runs completely each time the outer loop runs once, so the inner code runs outer x inner times.

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?
Repeating a sequence of instructions within a program loop.
What makes iteration count-controlled, and which Python loop does it?
A count value decides how many times the instructions repeat. In Python, a for loop implements it.
In for count in range(...), what does count do?
It takes each value in the range in turn, and the code inside the loop runs once for each value.
How does a for loop over a range finish?
It ends automatically when the end of the sequence is reached.
What does range() do when you give it one value?
That value is the stop and the sequence starts at 0. range(5) gives 0, 1, 2, 3, 4.
What do two values and three values mean in range()?
Two values: start and stop. Three values: start, stop and step. range(0, 10, 2) steps up by 2.
Is the stop value part of the sequence range() generates?
No. The stop value is never included; the loop never reaches it.
What kind of code can a for loop replace?
Repeated code, such as five print statements or copied input lines for each user.
Where can a loop's number of repeats come from?
A variable. For example a loop repeated num_users times, where num_users is entered by the user.
What are nested loops?
One loop placed inside another. The outer loop controls how many times the inner loop is repeated.
What does the inner loop do each time the outer loop runs once?
It runs completely, from its beginning.
An outer loop repeats 6 times and its inner loop repeats 3 times. How many times does the inner code run?
6 x 3 = 18 times.

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