That Blue Square Thing

AQA Computer Science GCSE

24 March 2020: as the position regarding GCSE exams is now a bit clearer, I've decided to stop adding updates to the revision pages.

Programming Concepts - Repetition

Indefinite Repetition - WHILE loops

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

WHILE loops are the most common way of doing this - and the only way that works in Python.

They continue until the condition specified by the loop control variable is no longer met. So, the block of code loops "WHILE the condition is True" and stops looping when it becomes False.

The pseudocode syntax is:

counter <- 0
WHILE counter < 4
OUTPUT counter
counter <- counter + 1
ENDWHILE
# outputs 0, 1, 2, 3

The Python syntax is similar:

counter = 0
while counter < 4:
print(counter)
counter = counter + 1
# outputs 0, 1, 2, 3

As always with Python, watch the indents and make sure you don't forget the colon (the :) at the end of the loop line.