11.2 Constructs

Bulk view disabled for Guests. View lessons individually.

Selection and Iteration

1. Selection (Decision Making)

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

IF Statements

Used for binary or simple multi-way branching.

IF Score >= 80 THEN
  Grade ← "A"
ELSEIF Score >= 70 THEN
  Grade ← "B"
ELSE
  Grade ← "C"
ENDIF

CASE Statements

Used when one variable is checked against multiple discrete values.

CASE OF DayNumber
  1 : Day ← "Monday"
  2 : Day ← "Tuesday"
  3 : Day ← "Wednesday"
  OTHERWISE Day ← "Error"
ENDCASE

2. Iteration (Loops)

Iteration repeats a block of code. Choosing the right loop is a key skill for Paper 2.

Count-Controlled: FOR Loop

Used when you know exactly how many times the code should run (e.g., iterating through an array).

// Iterating through an array of 10 items
FOR i ← 1 TO 10 STEP 1
  OUTPUT "Student " & i
NEXT i

Post-Condition: REPEAT...UNTIL

The code runs at least once. The condition is checked at the end. It repeats UNTIL the condition is TRUE.

REPEAT
  OUTPUT "Enter password:"
  INPUT Password
UNTIL Password = "1234"

Pre-Condition: WHILE...DO

The code might never run if the condition is false initially. It repeats WHILE the condition is TRUE.

WHILE Balance > 0 DO
  Balance ← Balance - ItemPrice
  OUTPUT "Purchased item."
ENDWHILE

3. Comparison Table

Loop Type Type Min Runs Use Case
FOR Count-controlled N/A Processing arrays, fixed ranges.
REPEAT Post-condition 1 Input validation, menus.
WHILE Pre-condition 0 File reading, searching.
⚠️ Exam Note: Infinite Loops

Ensure your loop has a terminating condition. In a WHILE or REPEAT loop, the variable used in the condition (e.g., Balance or Password) must be updated inside the loop body, otherwise the program will crash or hang.