Decision Statements
Choosing One Action with If-Else Statements
A program often needs to choose between two actions: charge a delivery fee or waive it, accept a booking or reject it, display one message or another. An if-else statement selects between two blocks of instructions using a condition. You will use Python examples here, but the same two-way choice appears in many programming languages.
One condition, two alternatives
A branch is one alternative block of instructions within a decision. When execution reaches an if-else statement, the program evaluates its condition. If the result is true, it runs the if branch. If the result is false, it runs the else branch. Exactly one branch runs each time the decision is reached.
Suppose a shop waives delivery fees for order totals of at least 60 dollars. Otherwise, delivery costs 7 dollars. If T is the order total in dollars, the rule is:
These two cases cover every permitted order total. An order cannot satisfy both cases at once, so there is no uncertainty about which fee applies.
Expressing the choice in Python
The shop can implement its rule as follows:
if order_total >= 60:
delivery_fee = 0
else:
delivery_fee = 7
print(delivery_fee)The comparison >= means greater than or equal to. Each header ends with a colon. Indentation shows which instructions belong to each branch, and else aligns with its matching if.
The final print line is outside both branches because it returns to the same indentation level as if. After the chosen branch finishes, execution continues there. The program prints a fee regardless of which branch ran.
The else header has no condition of its own. Python reaches that branch precisely when the if condition is false. Repeating a comparison after else is unnecessary and is invalid Python syntax.
Example: For an order total of 75 dollars, 75 >= 60 is true. The program sets delivery_fee to 0, skips the other assignment, and prints 0.
Checking the boundary and following execution
The equality case deserves special attention because everyday wording determines where it belongs. “At least 60” includes 60; “more than 60” excludes it. Changing >= to > would change the fee for an order worth exactly 60 dollars.
Order total in dollars | Result of the condition | Branch selected | Delivery fee in dollars |
|---|---|---|---|
59 | False | else | 7 |
60 | True | if | 0 |
61 | True | if | 0 |
A trace helps you check the program. Begin with the input, evaluate the condition, follow only the selected branch, and then continue with the instructions after the decision. Keep each variable's current value visible as you go.
What to remember
- A true condition selects the if branch; a false condition selects the else branch.
- Indentation determines which instructions belong to each branch.
- Instructions after the decision run after either branch finishes.
- Check whether the rule includes equality at its threshold.
Example: Calculating a parking charge
A car park charges 6 dollars for a stay of up to 3 hours and 10 dollars for a longer stay. It adds a 2-dollar booking charge to either parking fee. Assume hours is a positive whole number and all money values in the code are in dollars.
Use an execution trace, a record of the instructions that run and the values they produce, to follow this Python program for each booking.
if hours <= 3:
parking_fee = 6
else:
parking_fee = 10
booking_charge = 2
total = parking_fee + booking_chargeThe booking charge is added after either branch finishes.
a) Find the total charge for a 2-hour stay.
First substitute the stay length into the condition. The expression 2 <= 3 is ________, so the selected instruction is ________. The else assignment is skipped.
The parking fee is therefore 6 dollars. Execution continues after the decision, where the booking charge is set to 2 dollars. Add these two amounts:
The total charge for the 2-hour stay is 8 dollars.
b) Find the total charge for a 5-hour stay.
Substitute the new stay length into the same condition. The expression 5 <= 3 is ________, so the program skips the if assignment and selects the else branch. The parking fee becomes ________.
The booking charge is still 2 dollars because its assignment is outside the decision. Add it to the selected parking fee:
The total charge for the 5-hour stay is 12 dollars. Only the selected parking fee contributes to each total; the two alternative parking fees are never added together.
A museum evaluates the following Python code separately for each visitor. Complete the table with the exact admission price in dollars for each age, entering integers only.
if age >= 18:
price = 12
else:
price = 7| Visitor age in years | Admission price in dollars |
| 16 | |
| 17 | |
| 18 | |
| 21 |
A hypothetical electricity plan uses one rate for all monthly energy use, then adds a 4-dollar service charge. This is not tiered pricing. For the input shown, what bill does the Python code calculate? Answer in dollars to two decimal places.
energy_kwh = 120
if energy_kwh <= 100:
rate = 0.20
else:
rate = 0.15
bill = energy_kwh * rate + 4