8.1 Programming

Show All Section Notes

Basic Constructs

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.