Sequence, Selection and Iteration
Every algorithm ever written - from a sorting function to a search engine - is built from exactly three programming constructs. Master these and you can read, write and trace any algorithm.
In 1966, two computer scientists - Bohm and Jacopini - proved something remarkable: any computable problem can be solved using just three control structures. You do not need dozens of special keywords or obscure constructs. Just three. Sequence. Selection. Iteration. Every program you have ever run is ultimately an arrangement of these three patterns, no matter how complex it appears on the surface.
Sequence is the simplest construct: statements execute one after another, in the exact order they appear. There is no branching, no repetition - just a straight-line execution from top to bottom. It is the default behaviour of any algorithm unless selection or iteration keywords appear.
Even the most complex programs ultimately reduce to sequences of instructions at the hardware level. Everything else is just a way of controlling which sequences run in which order.
// A simple sequential calculation (AQA) OUTPUT "Enter length: " length ← USERINPUT OUTPUT "Enter width: " width ← USERINPUT area ← length * width perimeter ← 2 * (length + width) OUTPUT "Area: " + area OUTPUT "Perimeter: " + perimeter
# A simple sequential calculation (OCR) print("Enter length: ") length = input() print("Enter width: ") width = input() area = length * width perimeter = 2 * (length + width) print("Area: " + area) print("Perimeter: " + perimeter)
If you swap two lines of a sequential algorithm, the result often changes - or the algorithm breaks entirely. You cannot calculate area = length * width before you have assigned values to length and width. This seems obvious, but in an exam trace question, order errors are a common mistake to watch for.
Selection allows an algorithm to choose between different paths based on whether a condition is true or false. Without selection, every user would get the same output regardless of their input, which makes most real programs impossible.
Selection is implemented with IF/ELSE or CASE (switch) statements. The condition is always a Boolean expression - one that evaluates to either True or False.
Basic IF/ELSE:
score ← USERINPUT IF score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
score = input() if score >= 50 then print("Pass") else print("Fail") end if
Nested IF (ELSEIF) for multiple grades:
score ← USERINPUT IF score >= 90 THEN OUTPUT "Grade A" ELSEIF score >= 70 THEN OUTPUT "Grade B" ELSEIF score >= 50 THEN OUTPUT "Grade C" ELSE OUTPUT "Fail" ENDIF
score = input() if score >= 90 then print("Grade A") elseif score >= 70 then print("Grade B") elseif score >= 50 then print("Grade C") else print("Fail") end if
Common error: students write conditions in the wrong order for graded thresholds. If you check score >= 50 before score >= 90, a score of 95 would match the first condition and be graded C, never reaching the A check. Always check conditions from the most restrictive (highest threshold) to the least restrictive.
Iteration repeats a block of code. This is essential for processing lists, validating input, searching, sorting - almost every non-trivial algorithm needs at least one loop. There are two types: count-controlled (FOR) and condition-controlled (WHILE).
Use a FOR loop when you know exactly how many times the loop should run before it starts. The loop counter is set to a start value, compared to an end value, and incremented automatically each iteration.
// Print 5 to 1 countdown (AQA) FOR i ← 5 TO 1 STEP -1 OUTPUT i ENDFOR OUTPUT "Blast off!"
# Print 5 to 1 countdown (OCR) for i = 5 to 1 step -1 print(i) next i print("Blast off!")
Use a WHILE loop when you do not know in advance how many iterations are needed - the loop continues until a condition becomes false. The condition is checked before the loop body executes, so if the condition is already false, the loop body never runs.
// Find the first power of 2 greater than 1000 (AQA) n ← 1 WHILE n <= 1000 n ← n * 2 ENDWHILE OUTPUT n // outputs 1024
# Find the first power of 2 greater than 1000 (OCR) n = 1 while n <= 1000 n = n * 2 end while print(n) # outputs 1024
Use FOR when you know the number of iterations: "process all 10 items", "run 5 times", "check all 26 letters".
Use WHILE when you do not know: "keep asking until valid input", "repeat until the list is sorted", "continue until we find the target".
A common exam question asks you to justify which loop type you chose. Always give a reason tied to whether the number of iterations is known in advance.
Watch for the "off-by-one" error in FOR loops. FOR i ← 1 TO 10 runs 10 times (i=1,2,...,10). FOR i ← 0 TO 10 runs 11 times. In exam questions, always count carefully: "how many times does this loop execute?" is frequently tested. Also know that a WHILE loop may run zero times if its condition is false from the start - unlike a DO...WHILE loop which always runs at least once.
Real algorithms combine all three constructs. This example counts how many numbers entered by the user are above average. It uses sequence (to store running totals), iteration (to process each input), and selection (to compare each value to the average).
// Count how many of 5 numbers are above the average (AQA) total ← 0 FOR i ← 1 TO 5 // ITERATION: repeat 5 times OUTPUT "Enter number " + i num ← USERINPUT total ← total + num // SEQUENCE: update running total ENDFOR average ← total / 5 OUTPUT "Average: " + average aboveCount ← 0 FOR i ← 1 TO 5 // Second pass to count above-average values OUTPUT "Enter number " + i num ← USERINPUT IF num > average THEN // SELECTION: compare to average aboveCount ← aboveCount + 1 ENDIF ENDFOR OUTPUT aboveCount + " numbers above average"
# Count how many of 5 numbers are above the average (OCR) total = 0 for i = 1 to 5 print("Enter number " + i) num = input() total = total + num next i average = total / 5 print("Average: " + average) aboveCount = 0 for i = 1 to 5 print("Enter number " + i) num = input() if num > average then aboveCount = aboveCount + 1 end if next i print(aboveCount + " numbers above average")
Press Step to execute one line at a time and watch the variables update. Press Reset to start over.
Test yourself
1. Which construct describes statements that execute one after another in the order they are written?
2. When should a FOR loop be preferred over a WHILE loop?
3. How many times does this loop execute? FOR i ← 1 TO 10 STEP 2
4. The following condition tests a grade: IF score >= 50 THEN output "C" ELSEIF score >= 70 THEN output "B". A student scores 85. What is output?
5. A WHILE loop begins: WHILE x > 0. Before the loop starts, x = -3. How many times does the loop body execute?
A student writes a program to calculate the factorial of a number n (n! = 1 x 2 x 3 x ... x n). They use the algorithm below. Trace it for n = 5 and state the output. Then explain one way the algorithm could fail and what input value would cause it.
n ← USERINPUT result ← 1 FOR i ← 1 TO n result ← result * i ENDFOR OUTPUT result
result starts at 1.
i=1: result = 1*1 = 1
i=2: result = 1*2 = 2
i=3: result = 2*3 = 6
i=4: result = 6*4 = 24
i=5: result = 24*5 = 120
Output: 120. Correct - 5! = 120.
Failure case: If n = 0, the FOR loop runs from 1 TO 0. Since 1 > 0, the loop body never executes and result remains 1. The output would be 1, which is actually mathematically correct (0! = 1 by convention), but if n is negative (e.g. -3), the loop still never runs and outputs 1 - but factorial is undefined for negative numbers, so the output is meaningless. The algorithm needs input validation to reject negative values.
Practice what you have learned
Three levelled worksheets. Download, print and complete offline.