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.
Grade ← "A"
ELSEIF Score >= 70 THEN
Grade ← "B"
ELSE
Grade ← "C"
ENDIF
CASE Statements
Used when one variable is checked against multiple discrete values.
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).
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.
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.
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. |
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.