Iteration: the three kinds of loop Cambridge IGCSE Computer Science (9–1) 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

  1. A count-controlled loop repeats a set number of times: FOR Count ← 1 TO 10, then the statements, then NEXT Count.
  2. 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.
  3. 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.
  4. Use FOR when the number of repeats is known. Use WHILE or REPEAT when it depends on something that happens during the loop.
  5. 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?

  1. For each value of Row, the inner loop runs four times.
  2. Row takes three values: 1, 2 and 3.
  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.
6 questions, about 2 minutes.

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.

  1. Which loop is used when the number of repeats is known in advance?
    • FOR … NEXT (the answer)
    • WHILE … ENDWHILE
    • REPEAT … UNTIL
    • IF … ENDIF

    It is count-controlled.

  2. Which loop always runs its statements at least once?
    • FOR … NEXT
    • WHILE … ENDWHILE
    • REPEAT … UNTIL (the answer)
    • none of them

    The condition is tested at the end.

  3. A loop starts WHILE Count < 5 DO, and Count holds 9. How many times do its statements run?
    • 0 (the answer)
    • 1
    • 4
    • 5

    The condition is tested first, and 9 < 5 is false.

  4. How many times does a loop FOR Index ← 1 TO 5 repeat?
    • 4
    • 5 (the answer)
    • 6
    • it never stops

    Index takes the values 1, 2, 3, 4 and 5.

  5. When does a REPEAT … UNTIL Total > 100 loop stop?
    • when Total is 100 or less
    • when Total is greater than 100 (the answer)
    • after 100 passes
    • straight away

    It stops when the condition becomes true.

  6. An outer loop runs 5 times and the loop nested inside it runs 6 times. How many times do the inner statements run?Stretch
    • 6
    • 11
    • 30 (the answer)
    • 56

    The inner loop runs 6 times for each of the 5 outer passes.

Still stuck on this one?Ask in the Papermunch Discord, or help someone else who is. Discord is for ages 13 and up.Join the server

Things you can type

Or go straight to

Or browse a shelf