Nested Decisions
Following a Nested Decision
Nested conditionals are conditional statements placed inside other conditional statements. You use them when one decision matters only after an earlier decision selects a particular path. For example, an admission program might check whether a visitor has a ticket before deciding which admission category applies. The central task is to follow the path the program actually takes: a condition written in the code is not necessarily evaluated every time the program runs.
When an inner decision can run
A branch is a group of statements selected by a conditional. An inner decision belongs to the branch containing it. The program evaluates an inner condition only if execution reaches that condition.
This Python program checks a ticket before checking age:
if has_ticket:
if age < 14:
print("Junior admission")
else:
print("Standard admission")
else:
print("Ticket required")Here, has_ticket stores a Boolean value. When it is True, the program enters the outer if branch and evaluates age < 14. When it is False, the program takes the outer else branch and never evaluates the age condition.
Has ticket | Age condition | Printed message |
|---|---|---|
True | True | Junior admission |
True | False | Standard admission |
False | Not evaluated | Ticket required |
Notice that “not evaluated” is different from False. A visitor without a ticket could have any age; the program simply does not inspect that age on this path.
Indentation shows which decision owns the code
Python uses indentation to group statements. In the example, the age check is indented inside the ticket check. Its two messages are indented one level farther.
Each else lines up with the if it belongs to. The indented else handles a ticket holder whose age condition is false. The leftmost else handles a visitor whose ticket condition is false. Moving an else changes which decision it belongs to and can change the program's result.
An inner decision can also appear inside an outer else branch. In that arrangement, the inner decision is reached when the outer condition is false. Its location determines the required outer outcome.
Trace one path at a time
To trace nested conditionals, start at the outer condition. Select its branch, then read only the statements inside that branch. If you reach another conditional, evaluate it using the current values. Continue until you leave the selected branch.
Assignments made before the inner decision still matter. If an outer branch changes a price and an inner branch changes it again, use the updated price. Do not restart from the original value.
Example: A visitor with a ticket who is exactly 14 enters the outer if branch. The comparison 14 < 14 is false, so the inner else prints Standard admission.
After a nested decision finishes, execution continues with the next statement outside it. A statement after the entire outer conditional is reached regardless of which branch was selected, provided execution has not otherwise ended.
What to remember
- The containing branch controls access to an inner decision.
- A skipped condition is not the same as a false condition.
- In Python, indentation identifies which if owns each else.
- Track updated values and continue after the outer conditional.
Example: Tracing Delivery Charges
A grocery service uses nested conditionals to calculate a delivery fee. The variable is_member is either True or False, and order_total is the order value in dollars. All fees below are also in dollars.
fee = 6
if is_member:
fee = 4
if order_total >= 50:
fee = 0
else:
fee = 2
fee = fee + 1The final line adds a packaging charge after the delivery decision. Trace each customer's fee independently from the start of the code.
a) Find the final fee for a non-member whose order total is 70 dollars.
The initial assignment sets fee to ________. Because is_member is False, the program skips the entire outer if branch. The fee after this skipped branch remains ________.
The order total is not checked on the non-member path. An order above the threshold does not activate an inner decision that execution never reaches.
The final line is outside the conditional, so it adds the packaging charge:
The customer's final fee is 7 dollars.
b) Find the final fee for a member whose order total is 35 dollars.
The program starts with fee = 6. The outer condition evaluates to ________, so execution enters that branch and replaces the fee with 4 dollars.
The inner condition checks whether 35 is at least 50. This is false, so the inner else replaces the fee with ________. The program does not add 2 dollars to the previous fee; the assignment replaces its value.
The final line then adds 1 dollar:
The customer's final fee is 3 dollars.
c) Find the final fee for a member whose order total is exactly 50 dollars.
The initial fee is 6 dollars. The true outer condition changes it to 4 dollars and allows the program to reach the order-total check.
For this customer, order_total >= 50 evaluates to ________. The comparison includes an order exactly equal to 50 dollars. The inner if therefore replaces the fee with ________, and its else is skipped.
The final packaging assignment still runs:
The customer's final fee is 1 dollar.
A practice app stores nonnegative integer counts named successes and attempts, with successes no greater than attempts. This Python code should report whether the success rate reaches 0.75, but it fails when attempts is zero.
rate = successes / attempts
if attempts > 0:
if rate >= 0.75:
print("Target met")
else:
print("Below target")
else:
print("No attempts")Complete the repair instructions. Choose inside or outside for the first blank, and before or after for the second.
Move rate = successes / attempts the true branch of if attempts > 0, and place that calculation the inner if rate >= 0.75.
A bicycle repair shop calculates charges using the Python code below. For each request, enter the final charge in dollars as an exact integer.
charge = 18
if needs_parts:
charge = 32
if urgent:
charge = 47
else:
charge = 12The inputs needs_parts and urgent each store either True or False.
| Request | needs_parts | urgent | Charge (dollars) |
| A | False | False | |
| B | False | True | |
| C | True | False | |
| D | True | True |