Skip to content
Brainlag

Sequence, selection and iteration

GCSE Computer Science Updated Wed 7 Oct 2026

Every program is built from three structures: sequence (one step after another), selection (choosing a path) and iteration (repeating). Exam questions ask you to write them, spot which is which, and trace what a loop does.

Part 1 of 3: Learn it

In short

  1. Selection uses IF, ELSE IF and ELSE (or CASE) to choose which code runs.
  2. A count-controlled loop (FOR) repeats a fixed number of times.
  3. A condition-controlled loop (WHILE, REPEAT UNTIL) repeats until a condition changes.

Where this is in your specification

Spec points: AQA 3.2.2, OCR 2.2.1, Edexcel Topic 6

BoardTopic: Programming in exam style
AQA 85253.2.1 to 3.2.11, 3.7.1
Edexcel 1CP2Topic 6 (and Topic 1 for trace tables)
OCR J2772.2.1 to 2.2.3, 2.3.1, 2.3.2
Eduqas C5007 Principles of programming; 10 Program construction
Cambridge IGCSE 04788 Programming
National 5 C816 75Software design and development: implementation, testing

Selection

IF mark >= 70 THEN
OUTPUT "Grade A"
ELSE IF mark >= 50 THEN
OUTPUT "Grade B"
ELSE
OUTPUT "Try again"
ENDIF

Only one branch runs. The conditions are checked from the top, so put the most specific test first. A CASE (or switch) statement is a neater way to choose between many fixed values, such as menu options.

Iteration

LoopKindUse when
FOR i = 1 TO 10count-controlledyou know how many times to repeat
WHILE conditioncondition-controlled, checked at the startit may need to run zero times
REPEAT ... UNTIL condition (do while)condition-controlled, checked at the endit must run at least once, such as asking for input

Nesting and trace tables

Structures can sit inside each other: an IF inside a loop, or a loop inside a loop. To follow what code does, use a trace table: one column for each variable (and for output), and a new row each time a value changes.

Quick check

Which kind of loop would you use to keep asking for a password until it is correct?

Show the answer

A condition-controlled loop, such as WHILE or REPEAT UNTIL.

Part 2 of 3: See it worked

Worked examples

Example 1

Trace this code and give the final value of total: total = 0, then FOR i = 1 TO 4, total = total + i * 2, NEXT i.

  1. i = 1: total = 0 + 2 = 2
  2. i = 2: total = 2 + 4 = 6
  3. i = 3: total = 6 + 6 = 12
  4. i = 4: total = 12 + 8 = 20

Answer: 20.

Example 2

n = 50 and count = 0. WHILE n > 1: n = n DIV 2 and count = count + 1. What is count at the end?

  1. n goes 50, 25, 12, 6, 3, 1
  2. count goes up once for each halving: 1, 2, 3, 4, 5
  3. When n = 1 the condition n > 1 is false, so the loop stops

Answer: count = 5.

Common mistakes

  • Using a FOR loop when the number of repeats is not known in advance.
  • Writing a WHILE condition that never becomes false, so the loop never ends.
  • Ordering IF tests so a general condition catches values meant for a later branch.
  • Off-by-one errors: FOR i = 1 TO 10 runs 10 times, not 9.
Quick check

How many times does FOR i = 3 TO 7 run?

Show the answer

5 times (3, 4, 5, 6, 7).

Part 3 of 3: Test yourself

Check yourself

Answer each one in your head or on paper first, then open it to check.

Which kind of loop would you use to keep asking for a password until it is correct?

A condition-controlled loop, such as WHILE or REPEAT UNTIL.

How many times does FOR i = 3 TO 7 run?

5 times (3, 4, 5, 6, 7).

What are the three basic programming structures?

Sequence, selection and iteration.

Practise this topicSelection and iteration, with exam-style questions

Jobs that use this

Each link opens the job profile on the National Careers Service (England). In the rest of the UK: My World of Work (Scotland), Careers Wales, nidirect careers (Northern Ireland).