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 = 1while count <= 3:print(count)count = count + 1print("Done")
| count | count <= 3 |
|---|---|
| 1 |
Step 1. count starts at 1. This first row is worked for you.
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
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.
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
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?
What does a condition-controlled loop do?
How do you write a condition-controlled loop in Python 3?
When is a while loop's condition tested, and what if it is False the first time?
What happens when a while loop's condition becomes False?
What makes a while loop infinite?
What is a counter-controlled loop?
Which values does i take in for i in range(4):?
Which range counts from 1 to 10, including 10?
What does a negative step do in range(start, stop, step)?
What does for letter in word: do?
How do you write a counting loop with while?
Which loop for 'ask again until the answer is acceptable'?
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 Eduqas GCSE Computer Science topics
- Designing a testing strategy
- Explaining test outcomes
- File handling: open a file
- File handling: read into a variable
- File handling: read into an array
- File handling: write to a file
- Lists, tuples and dictionaries
- Mathematical and logical operations in code
- Modular structuring with abstraction
- Presenting test outcomes with commentary
- Program a solution to requirements
- Refining code during development
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.