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.
# Counted loop — you know how many times
for i in range(5):
print(i) # 0 1 2 3 4
for mark in marks: # once per item, count taken from the data
total = total + mark
# Conditional loop — you do not know how many times
guess = ""
while guess != answer:
guess = input("Guess: ")
print("Correct")// Counted loop — you know how many times
for (int i = 0; i < 5; i++) {
System.out.println(i); // 0 1 2 3 4
}
for (int mark : marks) { // once per item, count taken from the data
total = total + mark;
}
// Conditional loop — you do not know how many times
String guess = "";
while (!guess.equals(answer)) {
guess = scanner.nextLine();
}
System.out.println("Correct");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.
Trace a loop with a table: one row per pass, and check the condition every time before the body runs.
Memorize terms 3x faster
Smart flashcards show you cards right before you forget them. Perfect for definitions and key concepts.
# Counting only what matches
passes = 0
for mark in marks:
if mark >= 50:
passes = passes + 1
# Stopping early once the answer is found
found = False
for name in names:
if name == target:
found = True
break # no reason to keep looking
# Validating input: repeat until it is acceptable
age = -1
while age < 0 or age > 120:
age = int(input("Age: "))// Counting only what matches
int passes = 0;
for (int mark : marks) {
if (mark >= 50) {
passes = passes + 1;
}
}
// Stopping early once the answer is found
boolean found = false;
for (String name : names) {
if (name.equals(target)) {
found = true;
break; // no reason to keep looking
}
}
// Validating input: repeat until it is acceptable
int age = -1;
while (age < 0 || age > 120) {
age = Integer.parseInt(scanner.nextLine());
}if inside a loop
Runs the test once per item, so it counts, filters or finds.
The counter for what matched belongs outside the loop — inside, it would reset every time round.
break — leave early
Stops the loop immediately. Right when there is genuinely nothing left to learn, as in a search that has found its target.
It turns a search that always costs n into one that usually costs less.
Nested loops
A loop inside a loop runs the inner one completely for each pass of the outer.
That is why a grid costs rows × columns — and why nested loops are where O(n²) comes from.
Validation loops
while the input is unacceptable, ask again.
Start the variable at a value that fails the test, so the loop is guaranteed to run at least once.
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 answer broken into clear points, with guidance on how each point earns marks.