That Blue Square Thing

AQA Computer Science GCSE

September 2020: This page is the for the 2022 exam version of the AQA Computer Science GCSE course. This is for Year 10 teaching in September 2020.
These pages were copied over from the old syllabus and will get updated as I work through the new one. That means they won't always be totally up to date and I might miss something - check the updated date at the bottom of the page.

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
# outputs 0, 1, 2, 3

The Python syntax is similar:

counter = 0
while counter < 4:
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.