Reading If and Else Branches
A conditional runs work for the input cases described by its test and can supply a separate action for the other cases.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 2 min
In this article4 chapters
Write the two cases
For a capacity of eight seats, a queue can admit a guest whenever the occupied count is less than eight. At count seven it records an admission. At count eight the other branch leaves the count alone and puts the guest on a waiting list. Comparison boundaries decide where that change occurs.
Follow the shared result
If both branches assign an outcome, later code can report it once. Trace the assigned outcome and seat count independently for seven and eight. A hand trace makes it clear that the test runs before the update.[1][2]
Run an empty, a full, and an ordinary case
A workshop can accept another attendee if occupied seats are strictly less than capacity. For capacity five and occupied four, the true branch admits the attendee and updates the count to five. For occupied five, the false branch keeps the count there and records a waiting entry. These are two separate traces starting from known counts. If capacity is genuinely zero, both the before and after count stay zero. If zero capacity is invalid in the application, validation should reject it earlier. See inclusive and exclusive comparisons.
Separate the decision from the work it controls
You can first compute an eligibility flag and then handle the branch that uses it. This is useful when the predicate names a domain question and can be checked separately. A routine that reads the old count determines eligibility, and the update must preserve the expectation that occupied seats cannot exceed capacity. In an application where several requests can act concurrently, use an atomic reservation mechanism at the storage boundary. A useful test observes the selected result and the new count after both branches. Function postconditions explain why output text alone would be insufficient.