GCSE · Computer Science · Eduqas · Spec C500QS

Iteration: condition and counter controlled loops

Be the computer for ten minutes. Call every count and every True or False before the page shows it, and you will never mix up a loop again.

Computer Science · Loops

Be the computer: trace the while loop

You are the computer. Call each marked step before the table shows it. Then switch the starting value of count and run the same program again.

count = 1
while count <= 3:
print(count)
count = count + 1
print("Done")
countcount <= 3
1

Step 1. count starts at 1. This first row is worked for you.

Step 1 of 12Called right: 0 of 4

Read the test as a question the computer asks before every pass: is count <= 3 right now?

Predict, then check

Counting down in threes. Say the list in your head before you pick.

A for loop uses range(12, 0, -3). Which values does i take, in order?

Computer Science · Loops

Rewrite the for loop as a while loop

Rewrite for i in range(1, 4): print(i) so that it does the same job with while. Fill in the three missing lines.

  1. # prints 1, 2, 3 using whileA for loop does its counting for you. A while loop leaves the counting to you.
  2. missing step
Which line is step 2?

Exam line: Counter before the loop, counter in the condition, counter changed inside the body. Miss the last one and the loop never ends.

Computer Science · Loops

Find the off-by-one slip

This program should add up the whole numbers from 1 to 10 and print 55. It prints 45. Which line is wrong?

A student's program — which line goes wrong?

Exam line: To include 10 in a range, write 11 as the stop value.

Computer Science · Loops

When does a while loop actually stop?

lives = 1 while lives > 0: lives = lives - 1 print(lives)

Which idea is closest to what you think happens when this loop runs?
How sure are you?

Exam line: The condition is tested before each repetition. The loop can only end at a test.

Computer Science · Loops

Which loop would you choose?

Sort each job into the loop that fits it best, then read why.

Still to sort

Counter-controlled (for) (0)

The number of repetitions, or the sequence of values, is known before the loop starts.

Where the line is: If you could say the number of repetitions before running, it belongs here.

Condition-controlled (while) (0)

The number of repetitions is not known in advance and depends on a condition.

Where the line is: If the loop keeps going until something happens, it belongs here.

8 of 8 still to sort.

Exam line: Ask one question: do I know how many repetitions before the loop starts?
Watch out: A counting loop can also be written with while, so the choice comes from whether the number of repetitions is known in advance, not from the loop's counter.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

The computer does exactly what the code says, including skipping a loop completely. Let's find out when it tests, when it stops and how many times it runs.

What you need to know

  • Iteration repeats a block of instructions, called the body. A condition-controlled loop repeats it while a condition (a Boolean expression) is True.
  • In Python 3 you write while condition: and indent the body beneath it. The condition is tested before every repetition.
  • A False first test means zero runs of the body. A False test later ends the loop, and the program carries on after the body.
  • Have a goA classmate swears that x = 7 then while x < 3: print(x) prints 7 once, 'just to check'. How many times does print run?

    Zero times.

    7 < 3 is False at the very first test, and a while loop tests before it runs the body, so there is no 'just to check' pass.

  • The body must change something the condition depends on, like a variable or new input, or the loop repeats forever.
  • A counter-controlled loop repeats a set number of times, fixed before it starts. for i in range(n): gives i the values 0 to n - 1.
  • range(start, stop) stops before stop, so range(1, 6) gives 1 to 5. To include 10, use range(1, 11).
  • Have a goWrite the for line that gives i every value from 1 to 20, including 20.

    for i in range(1, 21):

    range stops before its stop value, so the stop has to be 21 for 20 to be included.

  • range(start, stop, step) changes the counter by step each time, and a negative step counts down: range(10, 0, -2) gives 10, 8, 6, 4, 2.
  • A for loop can also go through a sequence: for letter in word: runs the body once for each character.
  • Have a goHow many times does the body run in for ch in "python":?

    6 times.

    A for loop over a string runs the body once for each character, and python has six characters.

  • You can write a counting loop with while: set the counter first, test it in the condition, and change it inside the body.
  • Use for when the repetitions or values are known beforehand. Use while when a condition that can change decides.

The big picture

A loop repeats a block of instructions. A while loop keeps going while its condition is True and tests that condition before every repetition, so it can run zero times, and it needs its body to change something or it never ends. A for loop with range repeats a set number of times, and range stops one before its stop value. Choose for when you know the repetitions in advance, and while when a changing condition decides.

Key points

1A while loop tests its condition before every repetition, so its body can run zero times.
2A while loop only ends at a test. The rest of the body still finishes after the condition turns False.
3The body of a while loop must change something the condition depends on, or the loop is infinite.
4for i in range(n) gives i the values 0 to n - 1, and range(start, stop) stops before stop.
5Choose for when the repetitions are known in advance, and while when a changing condition decides.

Worked example

Problem

What does this program print, and how many times does the body run? n = 10 while n > 4: n = n - 3 print(n)

⚠ Watch out

Thinking a while loop always runs once, or that it stops the instant the condition turns False partway through the body. The test happens only before each repetition. Also watch range: it stops before its stop value, so range(1, 10) never gives 10.

🧠

Memory hook

While: test, body, test, body, and it only ends at a test. For with range: it never touches the stop value.

✓

Check yourself

Without running anything: which values does i take in for i in range(3, 12, 4), and is 12 one of them? Say why before you check.

Flashcards

(13)
What is iteration?
Repeating a block of instructions. The block that repeats is called the body of the loop.
What does a condition-controlled loop do?
It repeats its body while a condition (a Boolean expression) is True. The number of repetitions need not be known before it starts.
How do you write a condition-controlled loop in Python 3?
while condition: followed by the body, indented beneath the while line.
When is a while loop's condition tested, and what if it is False the first time?
Before every repetition. If it is False the first time, the body runs zero times.
What happens when a while loop's condition becomes False?
The loop ends and execution continues with the first statement after the indented body.
What makes a while loop infinite?
Nothing in the body changes what the condition depends on (a variable or new input), so the condition can never become False.
What is a counter-controlled loop?
A loop that repeats its body a set number of times, fixed before it starts, using a counter that takes a new value each repetition.
Which values does i take in for i in range(4):?
0, 1, 2, 3. range(n) repeats n times, giving i the values 0 to n - 1.
Which range counts from 1 to 10, including 10?
range(1, 11). range stops before its stop value, so the stop must be one larger than the last value you want.
What does a negative step do in range(start, stop, step)?
It counts down. range(10, 0, -2) gives 10, 8, 6, 4, 2.
What does for letter in word: do?
It runs the body once for each character of the string word, taking each character in order.
How do you write a counting loop with while?
Set the counter before the loop, test it in the condition, and change it inside the body. Forget the change and the loop is infinite.
Which loop for 'ask again until the answer is acceptable'?
A condition-controlled (while) loop. The number of repetitions is not known in advance.

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

See the full Eduqas Computer Science curriculum →

How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)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 15 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.