GCSE · Computer Science · OCR · Spec J277
Iteration (count- and condition-controlled loops)
Some jobs repeat a known number of times. Some repeat until something changes. Python has a loop for each, and the trick is knowing what makes each one stop.
Computer Science · Iteration
Two loops. Two reasons to stop.
Part 1 repeats once for every number in a sequence. Part 2 keeps asking for a passcode until the right one is typed. Step through and watch what actually ends each loop.
Before you start: how many times will line 3 run? And how many times will line 8?
1 # Part 1: count-controlled2 for number in range(1, 13):3 print(number, "x 7 =", number * 7)45 # Part 2: condition-controlled6 passcode = input("Passcode: ")7 while passcode != "7391":8 print("Wrong. Try again.")9 passcode = input("Passcode: ")10 print("Unlocked")
| number | passcode | passcode != "7391" |
|---|---|---|
| 1 |
Output
1 x 7 = 7
Step 1: range(1, 13) makes the sequence 1, 2, 3 and so on up to 12. number takes the first value, 1, and the indented line runs once.
Use Next to go one step at a time. Keep asking yourself: what is going to stop this loop?
Predict, then check
No running it. Just you and the brackets.
What sequence does range(5) give?
Computer Science · Iteration
For loop or while loop?
Sort each job or description. Does it belong to count-controlled iteration or condition-controlled iteration?
Still to sort
Count-controlled: a for loop (0)
Repeats once for each value in a sequence: letters, list items, numbers in a range.
Where the line is: The number of repeats is set by the sequence or count, not by checking whether something is True.
Condition-controlled: a while loop (0)
Repeats while a condition is True.
Where the line is: It keeps going because a condition is still True, not because there are values left to work through.
Some jobs go through a sequence. Some jobs wait for something to change.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What you need to know
- Iteration means repeating a sequence of instructions inside a loop.
- Count-controlled iteration repeats a number of times set by a count value. In Python, that's a for loop.
- A for loop runs its code once for every element in a sequence: letters in a word, items in a list, or numbers in a range.
- Each time round, the loop variable takes the next value in the sequence.
Have a goMo reckons for letter in "banana": only runs 3 times, because there are only three different letters. How many times does it really run?
6 times, once for each letter.
The loop goes through the sequence element by element: b, a, n, a, n, a. Repeated letters still count; it doesn't skip ones it has seen.
- range() generates a sequence of numbers, and you can give it up to three values. range(5) gives 0, 1, 2, 3, 4.
- The stop value is never used. range(1, 13) gives 1 to 12, and the loop ends by itself when the sequence runs out.
Have a goQuick one: what is the last number printed by for n in range(3, 8): print(n)?
7
The sequence is 3, 4, 5, 6, 7. Picking 8 means letting the stop value in, and the stop value is never used.
- A third value makes range step through the numbers: range(2, 11, 2) gives 2, 4, 6, 8, 10.
- Condition-controlled iteration repeats based on whether a condition is True or False. In Python, that's a while loop.
- A while loop checks its condition at the start of every pass, and keeps repeating while it's True.
- If the condition isn't met at the very start, the code inside a while loop doesn't run at all.
Have a goscore is 50 when the program reaches while score < 10: Your friend bets it gets one go before anything is checked. How many times does the code inside run?
None. Your friend loses the bet.
50 < 10 is False, and the condition is checked before the first pass. There's no free go first: the loop is skipped.
The big picture
Iteration means repeating a sequence of instructions in a loop. Count-controlled iteration uses a for loop: it runs once for each element in a sequence, such as letters in a word, items in a list or numbers from range, and it ends when the sequence runs out. range never includes its stop value, so range(1, 13) gives 1 to 12, and a third value sets a step. Condition-controlled iteration uses a while loop: it repeats while a condition is True. The condition is checked at the start of every pass, so if it isn't met at the start, the code inside never runs.
Key points
Worked example
Problem
How many times does the code inside for count in range(4, 20, 5): run, and what values does count take?
⚠ Watch out
Counting the stop value. range(1, 13) gives 1 to 12, so a loop over it runs 12 times, not 13. The while-loop version of the same slip: assuming the code inside runs once before the condition is checked. It doesn't, so a while loop can run zero times.
Memory hook
A for loop runs out. A while loop gives up. The for loop stops when there are no values left; the while loop stops when its condition is False, and it checks before it starts, so it can give up before the first go.
Check yourself
Cover the page. For each loop in the passcode program, say out loud what makes it stop. Then invent a range that would make a for loop run exactly 3 times.
Flashcards
(13)What is iteration?
What controls count-controlled iteration?
Which Python loop gives count-controlled iteration?
How many times does a for loop run its code?
Name three kinds of sequence a for loop can go through.
What happens to the loop variable each time round a for loop?
How does a for loop know when to end?
What does range do, and how many values can you give it?
Is the stop value included in a range?
What does a third value in range do?
What controls condition-controlled iteration?
When does a while loop check its condition?
Can a while loop run zero 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 postedMore OCR GCSE Computer Science topics
How this lesson was checked. This OCR GCSE Computer Science (specification J277)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 3 November 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.