A selection chooses one path from several based on a condition. To design it well, write the ranges first, then choose test values on and beside each boundary. This appears in questions about fares, grades, discounts and password rules.
It relies on tracing with a state table to prove which branch ran, and it connects to explaining algorithms independently of a language.
How do you design a selection?
- List the ranges the rule describes, without gaps or overlaps.
- Choose the conditions so each value falls into exactly one range.
- Order the tests from one end of the scale to the other.
- Pick boundary test data: the last value in one range and the first value of the next.
- Trace each test and compare with the expected result you wrote first.
Worked example
A bus fare depends on age.
Under 13: RM2. From 13 to 59: RM5. 60 and over: RM3.
Step 1, ranges: 0 to 12, 13 to 59, 60 upwards.
Step 2, algorithm:
INPUT Age
IF Age < 13 THEN
Fare ← 2
ELSE
IF Age < 60 THEN
Fare ← 5
ELSE
Fare ← 3
ENDIF
ENDIF
OUTPUT Fare
Step 3, boundary tests with expected results chosen before tracing:
| Age | Expected | Trace | Fare |
|---|---|---|---|
| 12 | child | 12 < 13 true | 2 |
| 13 | adult | 13 < 13 false, 13 < 60 true | 5 |
| 59 | adult | 59 < 13 false, 59 < 60 true | 5 |
| 60 | senior | 60 < 13 false, 60 < 60 false | 3 |
All four outputs match the expected fares. Both edges, 12 and 13, and 59 and 60, are covered.
The mistake to watch for
A common slip is the wrong comparison symbol at the boundary.
Mistaken condition:
IF Age <= 13 THEN Fare ← 2A 13-year-old now pays RM2, but the rule says 13 to 59 pays RM5.
Typical test ages such as 8, 30 and 70 all pass, so the fault stays hidden.
Only the boundary test at 13 shows it: 13 <= 13 is true, which gives RM2 instead of the expected RM5. The correction is Age < 13. Always test the value on the edge, not only values in the middle of each range.
Check yourself
1. A grade rule: score 80 or more gives A, 50 to 79 gives B, below 50 gives C. Give the output for 49, 50, 79 and 80.
Show answer
49 gives C. 50 gives B. 79 gives B. 80 gives A. These four values sit on both edges.
2. Delivery is free if the order total is more than RM100. What happens at RM100, and which comparison do you use?
Show answer
RM100 is not more than RM100, so delivery is not free. Use Total > 100. The boundary tests are 100 (not free) and a value just above, such as 100.01 (free).
3. A password must be 8 to 12 characters long, inclusive. Write the condition and the boundary lengths to test.
Show answer
Length >= 8 AND Length <= 12. Test 7 (rejected), 8 (accepted), 12 (accepted) and 13 (rejected).
Where this leads next
Next, practise describing your selections in plain steps in explaining an algorithm independently of a language. The pseudocode trace trainer shows which branch ran for each input.
Boundary mistakes are easy to miss alone, and a teacher can show you how to test your own designs in online one-to-one Computer Science tuition. The practice set has boundary questions at several levels.