Basic Constructs
0. The Three Basic Constructs
Every program ever written, in any language, is built from just three control structures. The syllabus names all three, and this lesson covers the second and third in detail — but the first is the one students forget to mention, precisely because it seems too obvious to be worth a name.
- Sequence — instructions are carried out one after another, in the order they are written. No decisions, no repetition. This is the default: unless something tells it otherwise, a program runs straight down the page.
- Selection — a decision is made, and different instructions run depending on the outcome (
IF,CASE). - Iteration — a set of instructions is repeated (
FOR,WHILE,REPEAT).
1. Selection
Selection allows the program to choose different paths based on a condition.
Used for binary choices (Yes/No).
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Efficient for multiple discrete choices (e.g., a menu).
1 : CALL AddRecord()
2 : CALL DeleteRecord()
3 : CALL ViewRecord()
OTHERWISE OUTPUT "Invalid Choice"
ENDCASE
2. Iteration (Loops)
Iteration is used to repeat a block of code multiple times.
Count-controlled: Used when you know exactly how many times the loop should run.
OUTPUT "Iteration: ", Count
NEXT Count
Post-condition: The condition is checked at the end. The loop always runs at least once.
OUTPUT "Enter a positive number"
INPUT Num
UNTIL Num > 0
Pre-condition: The condition is checked at the start. If the condition is false initially, the loop never runs.
INPUT Answer
ENDWHILE
3. Comparison Table: Which Loop to Use?
| Loop Type | Best Used For... | Min. Iterations |
|---|---|---|
| FOR | Fixed number of repetitions. | Defined by range |
| REPEAT | Validation (checking input). | 1 |
| WHILE | Reading files or unknown repetitions. | 0 |
IF needs an ENDIF, every WHILE needs an ENDWHILE, and every CASE needs an ENDCASE.