Iteration: the three kinds of loop Cambridge IGCSE Computer Science revision
Not started
Learn it
In plain words
Iteration means repeating statements. There are three kinds of loop, and the choice depends on one question: do you know in advance how many times to go round?
5 things to know
- A count-controlled loop repeats a set number of times: FOR Count ← 1 TO 10, then the statements, then NEXT Count.
- A pre-condition loop tests its condition before each pass: WHILE Number > 0 DO, then the statements, then ENDWHILE. If the condition is false at the start, the statements never run at all.
- A post-condition loop tests its condition after each pass: REPEAT, then the statements, then UNTIL Number > 0. Its statements always run at least once, and it stops when the condition becomes true.
- Use FOR when the number of repeats is known. Use WHILE or REPEAT when it depends on something that happens during the loop.
- A nested loop is a loop inside another loop. The inner loop runs all the way through for each single pass of the outer loop.
Worked example
An outer loop runs FOR Row ← 1 TO 3. Inside it, an inner loop runs FOR Column ← 1 TO 4 and outputs a star. How many stars are output?
- For each value of Row, the inner loop runs four times.
- Row takes three values: 1, 2 and 3.
- 3 × 4 = 12 stars.
Tips and tricks
- WHILE checks first, so it can run zero times. REPEAT checks last, so it runs at least once. That one difference answers most questions on loops.
- REPEAT … UNTIL stops when the condition is true. WHILE carries on while the condition is true. They are opposite ways round.
It lands in your notebook with its questions as flashcards.
Iteration: the three kinds of loop: 6 questions and answers
These are the quiz’s questions. Do the quiz first, then come back here for the ones that got you.
Which loop is used when the number of repeats is known in advance?
It is count-controlled.
Which loop always runs its statements at least once?
The condition is tested at the end.
A loop starts WHILE Count < 5 DO, and Count holds 9. How many times do its statements run?
The condition is tested first, and 9 < 5 is false.
How many times does a loop FOR Index ← 1 TO 5 repeat?
Index takes the values 1, 2, 3, 4 and 5.
When does a REPEAT … UNTIL Total > 100 loop stop?
It stops when the condition becomes true.
An outer loop runs 5 times and the loop nested inside it runs 6 times. How many times do the inner statements run?Stretch
The inner loop runs 6 times for each of the 5 outer passes.
Quiz
6 questions
Tap an answer and you’ll see straight away whether it’s right, and why.
Worksheet
2 questions, 5 marks. Write your answers on paper, then check them.
Iteration: the three kinds of loop
Cambridge IGCSE Computer Science 0478 · 5 marks · papermunch.org
Name ______________________________ Date ______________
Name the three types of loop and give the pseudocode keywords that start each one.[3]
Show answerHide answer
Count-controlled: FOR. Pre-condition: WHILE. Post-condition: REPEAT.
Explain why a REPEAT … UNTIL loop is suitable for asking a user to enter a password until it is correct.[2]
Show answerHide answer
The user must be asked at least once, and a REPEAT loop always runs once before testing. It then repeats until the condition (a correct password) is true.
Answers: Iteration: the three kinds of loop
- 1. Count-controlled: FOR. Pre-condition: WHILE. Post-condition: REPEAT.
- 2. The user must be asked at least once, and a REPEAT loop always runs once before testing. It then repeats until the condition (a correct password) is true.



