A selection structure lets a program choose one path from several, based on a condition that is true or false. The skill is writing conditions that cover every input exactly once, then proving it with a trace.
This lesson belongs to SPM Computer Science programming foundations. If you are unsure which values a variable can hold, read distinguishing variables, constants and data types first.
What does an IF, ELSE IF, ELSE chain actually do?
The program tests the conditions from the top. It runs the block under the first condition that is true, then skips the rest of the chain.
That “first true wins” rule explains most selection bugs. The order of your conditions is part of the logic, not a matter of style.
Worked example: a bus fare
A fictional town bus charges RM0 for children below 7, RM1 for ages 7 to 12, and RM2 for everyone aged 13 and above.
INPUT age
IF age < 7 THEN
fare = 0
ELSE IF age <= 12 THEN
fare = 1
ELSE
fare = 2
END IF
OUTPUT fare
The second condition does not need to say “age >= 7”. A child who reached it already failed the first test, so age is at least 7.
Now trace the boundaries, not just a comfortable middle value.
| age | age < 7 | age <= 12 | fare |
|---|---|---|---|
| 6 | true | not tested | 0 |
| 7 | false | true | 1 |
| 12 | false | true | 1 |
| 13 | false | false | 2 |
Ages 6, 7, 12 and 13 sit on either side of the two boundaries. If all four give the fares in the problem, the chain is correct where it matters.
The mistake that sends inputs to the wrong branch
A common slip is to write the broad condition first.
IF age < 13 THEN
fare = 1
ELSE IF age < 7 THEN
fare = 0
ELSE
fare = 2
END IF
Trace age 5. The first condition, age < 13, is true, so fare becomes 1 and the chain ends. The test for age < 7 is never reached, so every child under 7 pays RM1.
The code runs without any error message, and it still gives wrong answers. Only a trace with a value below 7 exposes it.
| Step | Wrong chain | Correct chain |
|---|---|---|
| First test | age < 13 (broad) | age < 7 (narrow) |
| Age 5 result | fare 1 | fare 0 |
| Why | first true wins | narrow case caught first |
Order chains from the most specific condition to the most general, or make each condition a complete range such as age >= 7 AND age <= 12.
Choosing between one chain and several IFs
Use one chain when an input must land in exactly one group, as with fares. Use separate IF statements when several things can be true together, such as adding RM1 for luggage and RM1 for a bicycle on the same trip.
If you use separate IFs where a chain is needed, an input can trigger two blocks. That is another common selection fault.
Check yourself
A quiz marks a score out of 20. A score of 16 or more gives “Gold”, 10 to 15 gives “Silver”, and anything below 10 gives “Try again”.
Write the selection structure, then trace the scores 16, 15, 10 and 9.
Answer
INPUT score
IF score >= 16 THEN
OUTPUT "Gold"
ELSE IF score >= 10 THEN
OUTPUT "Silver"
ELSE
OUTPUT "Try again"
END IF
Score 16: first condition true, output Gold. Score 15: first false, second true, output Silver. Score 10: first false, second true, output Silver. Score 9: both false, output Try again.
The four traces show each boundary lands in the branch the problem describes.
What to study next
Selection decides which path runs once. To repeat a path, continue with writing loops with correct boundaries, then check your understanding with the programming foundations practice set.
For guided tracing on your own code, see online one-to-one Computer Science tuition.