Nested if Statements#
Note
Source: Adapted from the C# edition (decisions/ifnested.rst).
Python-specific indentation guidance is original. Loan-approval and
shipping-cost examples replace the original C# examples.
An if body can itself contain another if statement. This is
called nesting. Python’s indentation makes the nesting level visually
obvious.
Example: Loan Approval#
A simple loan approval process requires both a minimum credit score and sufficient income:
credit_score = int(input("Credit score: "))
annual_income = float(input("Annual income: $"))
if credit_score >= 650:
if annual_income >= 30000:
print("Loan approved.")
else:
print("Denied: income too low.")
else:
print("Denied: credit score too low.")
The outer if checks the credit score; only if that passes does the
inner if check the income.
Reading Nested Code#
Indentation levels tell you which else belongs to which if:
if a:
if b:
print("both a and b")
else:
print("a but not b")
else:
print("not a")
The else at indentation level 4 belongs to if b:.
The else at indentation level 0 belongs to if a:.
Flattening Nested if With and#
When nesting exists only to combine conditions, you can often flatten it
using and:
if credit_score >= 650 and annual_income >= 30000:
print("Loan approved.")
else:
print("Denied.")
This is simpler when you do not need separate messages for each failure reason.
Example: Shipping Cost#
Shipping cost depends on both weight and destination zone:
def shipping_cost(weight, zone):
"""Return shipping cost based on weight and zone (1 or 2)."""
if zone == 1:
if weight <= 5:
return 3.99
else:
return 3.99 + 0.50 * (weight - 5)
else: # zone == 2
if weight <= 5:
return 6.99
else:
return 6.99 + 0.75 * (weight - 5)
Nesting is appropriate here because the cost formula differs by zone.
Avoiding Excessive Nesting#
Deep nesting (three or more levels) is hard to read. Consider these refactoring options:
Extract nested logic into a helper function.
Use
and/orto combine conditions.Use
elifto flatten a chain of nestedif/elsestatements.
# deep nesting — harder to follow
if a:
if b:
if c:
do_thing()
# flattened with and — clearer
if a and b and c:
do_thing()