The big idea: A loop repeats a block of code.
The only real question is what decides when to stop: a count you already know, or a condition you keep testing.
A counted loop of 5 runs 0 to 4: Five values, starting at 0 and stopping before 5.
The same rule as string slices, and the same source of off-by-one errors.
Free preview
This is the free notes preview
You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:
- FlashcardsLock in vocabulary and key terms with spaced repetition.
- Practice questionsAnswer exam-style questions and get instant AI marking.
- Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
- Personalised study planA daily plan built around your exam date and weak areas.
Counted — for
- The number of repeats is known before it starts
- The counter is managed for you, so it cannot be forgotten
- Cannot loop forever by accident
- Right for: every item in a list, a fixed number of attempts, rows in a grid
Conditional — while
- Repeats as long as a condition holds
- May run any number of times, including none at all
- You must change what the condition depends on
- Right for: waiting for valid input, reading until end of file, repeating until solved
The choosing question: Do I know how many times before I start?
Yes → counted loop. No → conditional loop. Answering it first prevents most loop problems.
A while loop may run zero times: The condition is tested before the first pass.
If it is already false, the body never runs at all — which is correct behaviour, and is often exactly what you want.
Memorize terms 3x faster
Smart flashcards show you cards right before you forget them. Perfect for definitions and key concepts.
if inside a loop
break — leave early
Nested loops
Validation loops
The infinite-loop check: In any while loop, point at the line that changes what the condition tests.
If you cannot find it, or it sits inside an if that might not run, the loop can go on forever.
How this is tested — you must choose the right loop and get the number of repeats exactly right. It comes up two ways:
Paper 2 — working with code
- Construct a loop for a described task, 3-5 marks
- Trace a loop and state the output
- Explain why a loop runs the wrong number of times
Paper 2 — the algorithmic-thinking question
- Choose counted or conditional from a description
- Explain how a loop terminates, with no code shown
The classic trap: Off by one on the repeat count. A counted loop of 5 runs 0 to 4, and a while loop tests before each pass — so it may run zero times. Trace the first and last passes explicitly.
A program asks for marks until the user types -1, then prints how many were entered and how many were passes (50 or more). Write it.
Model answer plan
See the mark-by-mark plan — for / against / judgement, with marking guidance — in study mode.