That Blue Square Thing

AQA Computer Science GCSE

Programming Concepts - Repetition

Indefinite Repetition - DO–WHILE loops

Indefinite repetition repeats a block of code until a condition of some kind is met.

DO loops don't exist in Python, but do in some other programming languages. The basic difference is that the loop control is put at the bottom of the list rather than at the top.

This means that the code inside the loop is guaranteed to execute at least once - the loop control isn't checked until the end of the run through the loop. This can be handy, but can be tricky to deal with.

DO–WHILE loops are very similar to REPEAT–UNTIL loops - but there is a key difference to note...

The syntax is:

counter <- 2
DO
counter <- counter * counter
OUTPUT counter
WHILE counter < 64
# will output 4, 8, 16, 32, 64

Note that the loop control (counter < 64) becomes True once counter reaches 64. This means that 64 is outputted and then the loop stops. Compare this with how the REPEAT–UNTIL loop works to see the key difference between the two loop types.