Chained Decisions
Choosing One Outcome with Else-If Chains
Programs often choose one result from several possibilities: a battery status, a delivery charge, or an achievement level. An else-if chain connects an initial if test to additional tests and an optional final else. You use it when the alternatives belong to one decision and you want at most one branch to run.
The selection rule
An else-if chain follows the first-match rule: test conditions from top to bottom, execute the branch belonging to the first true condition, and skip the remaining branches. A later condition might also be true, but it is not evaluated after an earlier match.
If every tested condition is false, the final else branch runs. The else has no condition of its own. If you omit it and every condition is false, no branch runs, and execution continues after the chain.
Expressing thresholds
Suppose a device reports a battery percentage p, assumed to be between 0 and 100 inclusive. Its status follows this rule:
In Python, elif means else-if. Align if, elif, and else, and indent the statements belonging to each branch.
if battery < 20:
status = "Low"
elif battery < 60:
status = "Moderate"
else:
status = "High"Example: For a battery percentage of 52, the first condition is false and the second is true. The selected status is "Moderate"; the else branch is skipped.
The second condition does not explicitly say that the percentage is at least 20. Reaching it already establishes that the first test failed. Each later branch inherits the failures of all earlier tests.
Java expresses the same selection rule using else if. Its conditions appear in parentheses, and braces group the statements in a branch.
Purpose | Python | Java |
|---|---|---|
Start the chain | if condition: | if (condition) { ... } |
Add another test | elif condition: | else if (condition) { ... } |
Supply a fallback | else: | else { ... } |
Checking boundaries and order
A comparison determines which branch includes a threshold. In the battery example, exactly 20 fails battery < 20 and reaches the second branch. Exactly 60 fails both tests and reaches else. Replacing < with <= changes which status receives an exact threshold value.
Order matters when conditions overlap. If you test battery < 60 before battery < 20, every low battery percentage matches the broader first condition. The later low-battery branch is never selected.
For increasing upper limits, place the smaller limit first. For decreasing lower limits, place the larger limit first. These arrangements let more specific ranges receive their intended outcomes before broader tests can capture them.
When checking a threshold-based chain, trace a value just below each threshold, exactly at it, and just above it. At each test, record true or false and stop at the first true result. This habit exposes both misplaced equality signs and incorrectly ordered conditions.
What to remember
- Conditions are tested from top to bottom until the first match.
- A selected branch skips every remaining branch in that chain.
- The final else handles inputs that fail all earlier tests.
- Check threshold equality and overlapping conditions before trusting the order.
Example: Selecting a Parcel Charge
A parcel service uses an else-if chain to assign one delivery charge. The parcel mass is positive and measured in kilograms; charges are in Canadian dollars, abbreviated CAD. A boundary value is an input at which the selected category can change. Here, the charge schedule has boundaries at 3 kg and 8 kg.
if mass <= 3:
charge = 6
elif mass <= 8:
charge = 10
else:
charge = 18a) Find the charge for a parcel with a mass of 7.5 kg.
First test the initial condition. The comparison 7.5 <= 3 is ________, so the first assignment is skipped.
Next, the comparison 7.5 <= 8 is ________. The middle branch assigns charge = 10, and the fallback is skipped. No charges are added together because this chain selects one branch.
The delivery charge is CAD 10.00.
b) Find the charge for a parcel with a mass of exactly 8 kg. Explain whether the fallback runs.
The first comparison, 8 <= 3, is ________, so execution reaches the second test. The comparison 8 <= 8 is ________ because <= includes equality.
The middle branch assigns charge = 10. Exactly 8 kg belongs to the middle category, so the fallback does not run. That fallback applies only when the mass exceeds 8 kg.
The delivery charge is CAD 10.00.
c) A dispatcher proposes the reordered chain below. Find its charge for a 2.5 kg parcel and compare it with the original schedule.
if mass <= 8:
charge = 10
elif mass <= 3:
charge = 6
else:
charge = 18In the proposed chain, the first comparison, 2.5 <= 8, is ________. It immediately assigns charge = 10 and skips the remaining branches.
Under the original schedule, the same mass satisfies 2.5 <= 3, so it receives a charge of CAD 6.00. The proposed order changes the result because every positive mass satisfying mass <= 3 also satisfies the earlier test mass <= 8.
The proposed charge is CAD 10.00, compared with CAD 6.00 under the original schedule. Keep the smaller upper limit first to preserve the intended categories.
A quiz app assigns a badge from an integer score s between 0 and 100. The rules are: below 40 earns "retry", 40 to 54 earns "participant", 55 to 69 earns "bronze", 70 to 84 earns "silver", and 85 or more earns "gold". The programmer wants a Python chain whose every test uses only the < operator. Put the lines of the chain in the order they must appear so that every score gets the correct badge.
Loading ordering steps…
A bicycle locker charges a fee according to the following Python code. The stay is measured in nonnegative whole minutes. Complete the table with the fee in credits for each stay, using exact integers.
if minutes <= 30:
fee = 0
elif minutes <= 90:
fee = 2
elif minutes <= 150:
fee = 5
else:
fee = 8| Stay (min) | Fee (credits) |
| 30 | |
| 31 | |
| 90 | |
| 91 | |
| 150 | |
| 151 |