Programming Constructs and Subroutines
The Three Constructs
Every program ever written — from a pocket calculator to a streaming platform — is built from just three programming constructs: sequence, selection and iteration. Understanding these is the foundation of computational thinking, and they appear in virtually every exam question about algorithms and code.
Sequence
Sequence is the simplest construct: instructions run one after another, top to bottom. Most programs combine all three constructs, but sequence is always the default — unless a selection or iteration construct changes the order of execution.
INPUT length
INPUT width
SET area = length * width
OUTPUT “Area is ” & area
Selection
Selection lets a program make decisions. An IF statement tests a condition and branches to the matching block of code. Selection is how programs respond differently to different inputs — a login system, a grade boundary checker, a discount calculator all use selection at their core.
INPUT grade
IF grade >= 70 THEN
OUTPUT “Merit”
ELSE IF grade >= 50 THEN
OUTPUT “Pass”
ELSE
OUTPUT “Fail”
END IF
Sequence runs instructions in order; selection chooses between paths using IF/ELSE; iteration repeats a block of code using FOR, WHILE or REPEAT…UNTIL. Every program is built from these three constructs alone.
Iteration
Iteration repeats a block of code. There are two types of iteration loop:
Count-controlled loops (FOR loops) repeat a fixed, known number of times. Use them when you know exactly how many repetitions are needed before the loop starts.
FOR i = 1 TO 5
OUTPUT i
NEXT i
Condition-controlled loops repeat until a condition becomes true (or while it remains true). Use WHILE when the check is at the top (pre-condition); use REPEAT…UNTIL when the check is at the bottom (post-condition — always executes at least once).
SET password = “”
REPEAT
INPUT password
UNTIL password = “secret123”
OUTPUT “Access granted”
A rogue value (also called a sentinel value) is a data value agreed in advance to mean “stop entering data.” It is used with condition-controlled loops when you don’t know in advance how many items the user will enter. A common rogue value is −1 for a list of positive numbers, or a blank string for text input.
SET total = 0
INPUT number
WHILE number <> -1
SET total = total + number
INPUT number
END WHILE
OUTPUT “Total: ” & total
Here −1 is the rogue value. When the user types −1, the WHILE condition becomes false and the loop ends. The rogue value itself is never added to the total.
Subroutines: Procedures and Functions
A subroutine is a named block of code that can be called from elsewhere in the program. Subroutines reduce repetition and make programs easier to read, test and maintain. There are two kinds:
A procedure carries out a task but does not return a value. A function carries out a task AND returns a value to the point where it was called. Think of a function like a maths function: you put something in, and a result comes back out. The exam paper writes any subroutine as Declare … End Subroutine, and calls it with call.
PROCEDURE greet(name)
OUTPUT “Hello, ” & name
END PROCEDURE
FUNCTION square(n)
RETURN n * n
END FUNCTION
call greet(“Alice”)
SET result = square(5)
OUTPUT result
A procedure performs an action but returns nothing. A function performs an action AND returns a value using RETURN. Functions are used when the calling code needs to use the result of the subroutine.
A local variable exists only inside the subroutine where it was created. Once the subroutine finishes, the variable is destroyed. A global variable exists for the entire lifetime of the program and can be read or changed from any subroutine. Using too many global variables makes programs harder to debug because any part of the code might change them unexpectedly.
Self-documenting code uses descriptive identifiers — names like studentAge, calculateTotal or isValidEmail — so that the code reads almost like plain English. This makes the code easier for other programmers (or your future self) to maintain. Alongside comments, self-documenting identifiers are a key good-practice technique.
Bad: SET x = y * z. Good: SET totalCost = unitPrice * quantity. Both produce the same result, but only the second version is self-documenting.
Large real-world programs are split into hundreds or thousands of subroutines. Each subroutine is tested independently — a technique called unit testing. If a bug is found later, the programmer only needs to look inside the relevant subroutine rather than searching through thousands of lines. This is why professional code is almost never written as one long block — decomposing problems into subroutines is a core professional skill.