← Computer Science

Iteration: the three kinds of loop

Iteration means repeating a block of statements instead of writing it out many times. This lesson covers the count-controlled FOR loop, the two condition-controlled loops WHILE and REPEAT, the crucial difference between them, and how loops can be nested inside one another.

Slide 1 / 4

What iteration is and why we use it

A loop repeats a block until it should stopblock of statementsrepeatwhen doneSTOPA loop needs a way to stop, or it runs forever
  • Iteration, or looping, repeats a block of statements so you do not have to copy the same lines over and over.
  • A loop needs a way to stop, otherwise it becomes an infinite loop that never ends.
  • Count-controlled loops repeat a fixed number of times that is known before the loop starts.
  • Condition-controlled loops repeat until some condition changes, when the number of repeats is not known in advance.

Check you've got it

0/4

1. How many times does the body of this loop run? FOR count <- 1 TO 5 OUTPUT count NEXT count

2. Which loop is guaranteed to execute its body at least once?

3. What value is output by this nested loop? total <- 0 FOR i <- 1 TO 3 FOR j <- 1 TO 2 total <- total + 1 NEXT j NEXT i OUTPUT total

4. A WHILE ... ENDWHILE loop is described as a pre-condition loop. What does this mean?

Practise real exam questions on this

Type your answer, see your mark against the mark scheme in seconds. Free, 5 a day.

Start practising →

Teaching this topic?

Mark your whole class's answers and see who's stuck, for £19 a month.

See the class plan →

Free revision content. Multiple-choice answers are marked instantly. Written answers are marked by AI on /free-mark.