Nested if Statements
Putting if statements inside other if statements — for decisions that depend on other decisions first.
"Some questions only make sense to ask after you already know the answer to another one. That's when you nest."
— ShurAIA Real-Life Nested Decision
Think about ordering food at a restaurant. You only decide what to eat after deciding whether you are hungry. And you only decide what to drink after deciding what food you ordered. One decision leads to the next:
That is exactly how nested if works in Python — an if inside another if.
Basic Syntax — Indent Further Each Level
is_hungry = True
wants_veg = False
if is_hungry:
print("You are hungry!")
if wants_veg: # ← inner if (8 spaces deep)
print("Ordering: Paneer Butter Masala")
else:
print("Ordering: Chicken Biryani")
else:
print("Just having a coffee.")
You are hungry!
Ordering: Chicken Biryani
Example 1 — Cinema Ticket Booking 🎬
First check if tickets are available. Only then check if the person qualifies for a discount:
tickets_left = 10
age = int(input("Your age: "))
if tickets_left > 0:
print("Tickets are available!")
if age < 12:
print("Child ticket: Rs.80 🎈")
elif age >= 60:
print("Senior ticket: Rs.100 🌟")
else:
print("Adult ticket: Rs.200 🎟️")
else:
print("Sorry! Show is fully booked. 😔")
Example 2 — Login System 🔐
First check the username. Only if that's right, check the password. This is how real login systems think:
correct_user = "admin"
correct_pass = "shurai123"
username = input("Username: ")
password = input("Password: ")
if username == correct_user:
if password == correct_pass:
print("✅ Login successful! Welcome, admin.")
else:
print("❌ Wrong password. Try again.")
else:
print("❌ Username not found.")
You could write if username == correct_user and password == correct_pass — and for simple cases that works great. Use nested if when each level needs to give a different error message or do something in between. Nested if = more specific responses.
Example 3 — Job Application Checker 💼
print("=== Job Application System ===")
age = int(input("Your age : "))
experience = int(input("Years of experience: "))
degree = input("Do you have a degree? (yes/no): ").lower()
if age >= 18:
if degree == "yes":
if experience >= 2:
print("🎉 Excellent! You qualify for Senior role.")
else:
print("👍 You qualify for Junior role.")
else:
if experience >= 5:
print("💪 Strong experience! Eligible for consideration.")
else:
print("📚 Consider completing a degree first.")
else:
print("⚠️ Minimum age requirement is 18.")
=== Job Application System ===
Your age : 25
Years of experience: 3
Do you have a degree? (yes/no): yes
🎉 Excellent! You qualify for Senior role.
Don't Go Too Deep — 3 Levels Max
Nesting is powerful, but going beyond 3 levels makes code very hard to read. If you find yourself going deeper, use and to combine conditions, or break it into a function:
# ❌ Too nested — hard to follow
if age >= 18:
if has_id:
if not is_banned:
if has_paid:
print("Enter!")
# ✅ Better — combine with 'and'
if age >= 18 and has_id and not is_banned and has_paid:
print("Enter!")
"Nested ifs are like Russian dolls — each one opens to reveal another. Useful up to a point, but keep it readable."
— ShurAI🧠 Quiz — Question 1
How many spaces of indentation does the inner if use when nested inside an outer if?
🧠 Quiz — Question 2
When would you choose nested if over combining with and?
🧠 Quiz — Question 3
The inner if only runs when:
🧠 Quiz — Question 4
What is the recommended maximum nesting depth?