Sequence, selection and iteration
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
- Selection uses IF, ELSE IF and ELSE (or CASE) to choose which code runs.
- A count-controlled loop (FOR) repeats a fixed number of times.
- 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
| Board | Topic: Programming in exam style |
|---|---|
| AQA 8525 | 3.2.1 to 3.2.11, 3.7.1 |
| Edexcel 1CP2 | Topic 6 (and Topic 1 for trace tables) |
| OCR J277 | 2.2.1 to 2.2.3, 2.3.1, 2.3.2 |
| Eduqas C500 | 7 Principles of programming; 10 Program construction |
| Cambridge IGCSE 0478 | 8 Programming |
| National 5 C816 75 | Software design and development: implementation, testing |
Selection
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
| Loop | Kind | Use when |
|---|---|---|
| FOR i = 1 TO 10 | count-controlled | you know how many times to repeat |
| WHILE condition | condition-controlled, checked at the start | it may need to run zero times |
| REPEAT ... UNTIL condition (do while) | condition-controlled, checked at the end | it 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.
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.
- i = 1: total = 0 + 2 = 2
- i = 2: total = 2 + 4 = 6
- i = 3: total = 6 + 6 = 12
- 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?
- n goes 50, 25, 12, 6, 3, 1
- count goes up once for each halving: 1, 2, 3, 4, 5
- 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.
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.
Jobs that use this
- Software developer (opens a new tab)
- Computer games developer (opens a new tab)
- Systems analyst (opens a new tab)
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).
Full lessons and marked practice for this course are coming soon to Brainlag Learn. See courses