Skip to content
IGCSE·Tuition
Computer Science · Lessons

Design a selection with boundary cases

A rule that works for most values can still fail for the one value that sits exactly on the edge.

On this page
  1. How do you design a selection?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

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?

  1. List the ranges the rule describes, without gaps or overlaps.
  2. Choose the conditions so each value falls into exactly one range.
  3. Order the tests from one end of the scale to the other.
  4. Pick boundary test data: the last value in one range and the first value of the next.
  5. 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:

AgeExpectedTraceFare
12child12 < 13 true2
13adult13 < 13 false, 13 < 60 true5
59adult59 < 13 false, 59 < 60 true5
60senior60 < 13 false, 60 < 60 false3

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 ← 2

A 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.

Questions people ask

What is a boundary value?

A boundary value is a test value at the edge of a range where the outcome changes, along with the values just either side. For a rule that changes at 60, the boundary tests are 59, 60 and 61, or 59 and 60 for whole numbers.

What is the difference between > and >=?

The symbol > means strictly greater than, so 60 > 60 is false. The symbol >= means greater than or equal to, so 60 >= 60 is true. They differ only at the boundary value, which is why that value must be tested.

Should I use nested IF or several separate IFs?

A nested IF with ELSE is safest when the ranges do not overlap, because exactly one branch runs. Several separate IF statements can run more than one branch if you are not careful, so check each case with a trace.

Updated:

Your next step

If your selections pass the sample data and then fail on one value, a one-to-one teacher can help you build a habit of testing the edges before anything else.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service