GCSE · Computer Science · Eduqas · Spec C500QS
Iteration
Here's a loop that runs three times. Give it a different starting number and it runs zero times. By the end, you'll call that before it happens.
Computer Science · Iteration
Call the trace: how many times does the WHILE body run?
Same loop, two runs. Predict each marked value before the table shows it, then switch to Run 2.
Step through with Next. At each marked step, make your call before the table fills in. The starting numbers are made up.
INPUT totalcount = 0WHILE total < 10total = total + 4count = count + 1ENDWHILEOUTPUT count
| total | count | total < 10 |
|---|---|---|
| 1 |
Step 1. INPUT gives total its first value: 1. This row is worked for you.
The row for total < 10 is the test. True means the body runs once more; False means the loop ends.
Two families of loop
Count-controlled or condition-controlled?
Pick a made-up task, then choose its loop type. Ask yourself: is the number of repetitions fixed before the loop starts, or does it depend on the data?
Still to sort
Count-controlled (e.g. FOR) (0)
The number of repetitions is fixed before the loop begins.
Where the line is: If you could write the number of repetitions down before the loop starts, it is count-controlled.
Condition-controlled (e.g. WHILE, REPEAT ... UNTIL) (0)
It repeats depending on whether a condition is true.
Where the line is: If the data decides when it stops, you cannot know the number of repetitions in advance.
Every one of these tasks repeats something. The question is what decides when it stops.
Computer Science · Algorithms
Trace table — dry run the code
A loop inside a loop. Step through and watch inner run all the way from 1 to 3, then start again for the next value of outer.
Ready when you are — step through one line at a time.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
How loops repeat code, and how to tell how many times a loop will actually run.
What you need to know
- Iteration means repeating a set of instructions, a block of code, in an algorithm or program.
- A count-controlled loop, such as FOR, repeats a fixed number of times, and that number is set before the loop begins.
Have a goYour mate insists a loop that prints a heading 4 times is condition-controlled, "because it stops when it's finished". Are they right?
No. It's count-controlled.
The 4 is fixed before the loop begins. Stopping isn't the test: what matters is whether the number of repetitions is known in advance.
- Its counter variable takes each value from the start value to the end value, and the body runs once for each value.
Have a goFOR k = 3 TO 7. How many times does the body run? Work it out before you look.
5 times (k = 3, 4, 5, 6 and 7).
k takes every value from the start to the end, both ends included. The tempting 4 comes from just doing 7 − 3.
- A condition-controlled loop, such as WHILE or REPEAT ... UNTIL, repeats depending on a condition, so the data decides how many repetitions happen.
- WHILE is a pre-condition loop: it tests the condition before each repetition and repeats while the condition is true.
Have a goA WHILE loop's condition is tickets_left > 0. Just before the next test, tickets_left is 0. Does the body run again?
No. The loop ends.
WHILE only repeats while its condition is true, and 0 > 0 is false. A yes comes from reading WHILE as 'repeat until'.
- Because WHILE tests first, a condition that is false at the very first test means the body runs zero times.
- REPEAT ... UNTIL is a post-condition loop: it tests after each repetition and repeats until the condition becomes true, so it always runs at least once.
- If every WHILE test is true, or every UNTIL test is false, the loop repeats indefinitely: an infinite loop. An unchanged tested value can cause this.
- In nested loops, the inner loop completes all its repetitions for each single repetition of the outer loop.
- A trace table follows a loop step by step, recording the value of each variable after every repetition.
The big picture
Iteration repeats a block of code. A FOR loop repeats a fixed number of times; WHILE and REPEAT ... UNTIL loops repeat depending on a condition. WHILE tests first and may run zero times; REPEAT ... UNTIL tests last and always runs at least once.
Key points
Worked example
Problem
n = 1, then REPEAT n = n × 3 UNTIL n > 20, then OUTPUT n. How many times does the body run, and what is output? What changes if n starts at 50 instead?
⚠ Watch out
Thinking every loop body runs at least once. A WHILE loop tests first, so if its condition is False at the very first test, the body runs zero times. Only REPEAT ... UNTIL is guaranteed one go.
Memory hook
WHILE waits at the door; REPEAT rushes in. WHILE checks before it goes in, so it might never go in at all. REPEAT goes in first and checks on the way out.
Check yourself
n = 7. WHILE n < 7 adds 1 to n: how many times does its body run? Now REPEAT that same body UNTIL n >= 7: how many times now, and why?
Flashcards
(13)What is iteration?
What makes a loop count-controlled?
In FOR i = 1 TO 4, which values does i take?
What makes a loop condition-controlled?
WHILE vs REPEAT ... UNTIL: when does each one test its condition?
What is the fewest number of times a WHILE body can run?
What is the fewest number of times a REPEAT ... UNTIL body can run?
A WHILE loop stops when its condition turns false. When does a REPEAT ... UNTIL loop stop?
What is an infinite loop?
How do you stop a condition-controlled loop running forever?
What are nested loops?
An outer loop repeats 4 times and the inner loop repeats 5 times. How many times does the inner body run?
What does a trace table record when you dry-run a loop?
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
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 30 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.