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-controlled
2 for number in range(1, 13):
3 print(number, "x 7 =", number * 7)
4
5 # Part 2: condition-controlled
6 passcode = input("Passcode: ")
7 while passcode != "7391":
8 print("Wrong. Try again.")
9 passcode = input("Passcode: ")
10 print("Unlocked")
numberpasscodepasscode != "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.

1 / 21

Use Next to go one step at a time. Keep asking yourself: what is going to stop this loop?

Step 1 of 21: 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..

Exam line: A for loop is count-controlled: it runs once for each value in its sequence and ends when the sequence ends. A while loop is condition-controlled: it repeats while its condition is True, checked at the start of each pass.

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.

6 of 6 still to sort.

Some jobs go through a sequence. Some jobs wait for something to change.

Computer Science · Iteration

Does a while loop always run at least once?

Same passcode program as before. This time the user types 7391 at the very first prompt. The program reaches while passcode != "7391":

What happens next? Pick the idea closest to yours.
How sure are you?

Computer Science · Iteration

Put it in writing

Describe the difference between count-controlled iteration and condition-controlled iteration. Give a suitable use of each. [4 marks]

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

Exam line: One idea per mark: what controls count-controlled iteration, what controls condition-controlled iteration, and a suitable use of each.

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

1Iteration repeats a sequence of instructions in a loop.
2Count-controlled iteration (for loop): once for each value in a sequence, ending when the sequence runs out.
3range never includes its stop value; a third value sets the step.
4Condition-controlled iteration (while loop): repeats while a condition is True, checked at the start of each pass, so it may not run at all.

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?
Repeating a sequence of instructions within a program loop.
What controls count-controlled iteration?
A count value, which determines the number of times the instructions are repeated.
Which Python loop gives count-controlled iteration?
A for loop.
How many times does a for loop run its code?
Once for every element in the sequence it is given.
Name three kinds of sequence a for loop can go through.
Letters in a word, items in a list, numbers in a range.
What happens to the loop variable each time round a for loop?
It takes the next value in the sequence.
How does a for loop know when to end?
It ends automatically when it reaches the end of the sequence.
What does range do, and how many values can you give it?
It generates a sequence of numbers to loop through. You can give it up to three values.
Is the stop value included in a range?
No. range(1, 13) gives 1 to 12.
What does a third value in range do?
It sets the step: range(2, 11, 2) gives 2, 4, 6, 8, 10.
What controls condition-controlled iteration?
Whether a condition evaluates as True or False. In Python it uses a while loop.
When does a while loop check its condition?
At the beginning of each loop, before the code inside runs.
Can a while loop run zero times?
Yes. If the condition isn't met at the start, the code inside doesn't run at all.

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 OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.