8.1 Programming concepts

Show All Section Notes
Test yourself on Programming concepts 14 questions — drag-to-order, code completion, matching and multiple choice.
Start the quiz

Basic Constructs

Watch this lesson Video 8.1.2 · 7:06 · Sequence, selection and iteration — and which loop to reach for

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).
Why sequence deserves a name. Because order changes meaning. Reading a value before using it in a calculation works; doing it the other way round does not. Many logic errors are not wrong instructions at all — they are correct instructions placed in the wrong sequence, which is why tracing a program line by line finds bugs that reading it does not.

1. Selection

Selection allows the program to choose different paths based on a condition.

IF ... THEN ... ELSE

Used for binary choices (Yes/No).

IF Mark >= 50 THEN
  OUTPUT "Pass"
ELSE
  OUTPUT "Fail"
ENDIF
CASE ... OF

Efficient for multiple discrete choices (e.g., a menu).

CASE Choice OF
  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.

FOR ... TO ... NEXT

Count-controlled: Used when you know exactly how many times the loop should run.

FOR Count ← 1 TO 10
  OUTPUT "Iteration: ", Count
NEXT Count
REPEAT ... UNTIL

Post-condition: The condition is checked at the end. The loop always runs at least once.

REPEAT
  OUTPUT "Enter a positive number"
  INPUT Num
UNTIL Num > 0
WHILE ... DO ... ENDWHILE

Pre-condition: The condition is checked at the start. If the condition is false initially, the loop never runs.

WHILE Answer <> "Exit" DO
  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
⚠️ Exam Note: Make sure to close your constructs! Every IF needs an ENDIF, every WHILE needs an ENDWHILE, and every CASE needs an ENDCASE.