Course Progress19% complete
🐍 Python Basics Topic 19 / 100
⏱ 8 min read

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."

— ShurAI

A 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:

IF you are hungry:
IF you want veg:
→ Order Paneer Butter Masala
ELSE (non-veg):
→ Order Chicken Biryani
ELSE (not hungry):
→ Just have a coffee

That is exactly how nested if works in Python — an if inside another if.

Basic Syntax — Indent Further Each Level

python
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.")
output
You are hungry!
Ordering: Chicken Biryani
Each level of nesting = 4 more spaces
if outer_condition: ← 0 spaces — level 1
    if inner_condition: ← 4 spaces — level 2
        do_something() ← 8 spaces — level 3
    else:
        do_other_thing()
else:
    fallback()

Example 1 — Cinema Ticket Booking 🎬

First check if tickets are available. Only then check if the person qualifies for a discount:

python
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:

python
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.")
Why Not Just Use and?

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 💼

python
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.")
terminal — example run
=== 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:

python — too deep vs flat
# ❌ 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?