Skip to content
SPM Tuition
Computer Science · Programming foundations

Writing selection structures that pick the right branch

Your IF statement reads fine, but the wrong branch runs when the input sits on a boundary.

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.

Common questions

When should I use ELSE IF instead of separate IF statements?

Use ELSE IF when the choices exclude each other, so only one branch should run. Separate IF statements are each tested on their own, so two of them can run for the same input. If an input must land in exactly one group, chain the conditions.

Does the order of conditions in an ELSE IF chain matter?

Yes. The program tests from the top and stops at the first true condition. A broad condition placed above a narrow one swallows the inputs meant for the narrow one, so test the most specific condition first.

Should I write < or <= at the boundary?

Decide from the wording of the problem. 'Below 7' means < 7, while 'up to 12' means <= 12. Then test the boundary value itself, for example 7 and 12, to confirm the branch you expected actually runs.

Do I need the final ELSE?

It is not compulsory, but it catches every input the earlier conditions missed. Leaving it out is fine only when you have checked that nothing else can arrive. A final ELSE also gives you a place to report an invalid input.

If your selection code works for ordinary inputs but breaks at the edges, one-to-one Computer Science lessons let a teacher trace your own code with you and find the exact line where the branch goes wrong.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.