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 total
count = 0
WHILE total < 10
total = total + 4
count = count + 1
ENDWHILE
OUTPUT count
totalcounttotal < 10
1

Step 1. INPUT gives total its first value: 1. This row is worked for you.

Step 1 of 10Called right: 0 of 3

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.

6 of 6 still to sort.

Every one of these tasks repeats something. The question is what decides when it stops.

Watch out: Repeating is not the test: every loop repeats. Ask whether you know how many repetitions there will be before the loop begins.

WHILE vs REPEAT ... UNTIL

WHILE (pre-condition loop)vsREPEAT ... UNTIL (post-condition loop)

Both are condition-controlled. What separates them is when the condition is tested.

Focus

When the condition is tested

WHILE (pre-condition loop)

Before each repetition

REPEAT ... UNTIL (post-condition loop)

After each repetition

The insight

This one difference causes everything else in the table. Pre means before; post means after.

Keeps repeating…

WHILE (pre-condition loop)

while the condition is true

REPEAT ... UNTIL (post-condition loop)

until the condition becomes true

Fewest times the body can run

WHILE (pre-condition loop)

Zero, if the first test is false

REPEAT ... UNTIL (post-condition loop)

Once, because the body runs before any test

Where the test sits in the code

WHILE (pre-condition loop)

At the top, on the WHILE line

REPEAT ... UNTIL (post-condition loop)

At the bottom, on the UNTIL line

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.

1total = 0
2FOR outer = 1 TO 2
3 FOR inner = 1 TO 3
4 total = total + 1
5 NEXT inner
6NEXT outer
7OUTPUT total
outer
inner
total
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Your turn to explain

Why won't this loop stop?

count = 1 WHILE count <= 5 OUTPUT count ENDWHILE

This loop is meant to output 1, 2, 3, 4 and 5 and then stop. Explain why it never stops, and state what must be added to fix it. [3 marks]

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

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

1Count-controlled (FOR): the number of repetitions is fixed before the loop begins.
2Condition-controlled (WHILE, REPEAT ... UNTIL): the data decides how many repetitions.
3WHILE: test before, so 0 or more repetitions. REPEAT ... UNTIL: test after, so 1 or more.
4If every WHILE test is true, or every UNTIL test is false, the loop repeats indefinitely. A body that never changes a value its condition tests can cause this.
5Nested loops multiply: the inner loop runs in full for every outer repetition.

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?
Repeating a set of instructions (a block of code) in an algorithm or program.
What makes a loop count-controlled?
It repeats a fixed number of times, set before the loop begins, like a FOR loop.
In FOR i = 1 TO 4, which values does i take?
1, 2, 3 and 4, one after another, with the body running once for each value.
What makes a loop condition-controlled?
It repeats depending on whether a condition is true, so the number of repetitions depends on the data and need not be known in advance.
WHILE vs REPEAT ... UNTIL: when does each one test its condition?
WHILE tests before each repetition (pre-condition). REPEAT ... UNTIL tests after each repetition (post-condition).
What is the fewest number of times a WHILE body can run?
Zero, when the condition is false at the first test.
What is the fewest number of times a REPEAT ... UNTIL body can run?
Once. The body runs before the condition is tested for the first time.
A WHILE loop stops when its condition turns false. When does a REPEAT ... UNTIL loop stop?
When its condition becomes true.
What is an infinite loop?
A loop that repeats indefinitely because its condition never changes in a way that ends it.
How do you stop a condition-controlled loop running forever?
Make the loop body change a value that the condition tests.
What are nested loops?
A loop placed inside another loop. The inner loop completes all its repetitions for each single repetition of the outer loop.
An outer loop repeats 4 times and the inner loop repeats 5 times. How many times does the inner body run?
20 times (4 × 5).
What does a trace table record when you dry-run a loop?
The value of each variable after every repetition, so you can follow the loop step by step.

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 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.