Nested conditionals
1.2.16· Coding › Introduction to Programming (Python)
WHAT is a nested conditional?
if outer_condition:
if inner_condition:
# sirf tab run hoga jab DONO True hon
...
else:
# outer True, inner False
...
else:
# outer False — inner kabhi check hi nahi hoga
...Key word hai indentation. Python mein, ek line kitni deep indent hai yeh decide karta hai ki woh kaunse if se belong karti hai. Yahi poora game hai.
WHY do we need them?
Tum kabhi kabhi nesting ko and se combined conditions mein flatten kar sakte ho, lekin nesting jeet jaata hai jab:
- Tum alag alag messages chahte ho har level par (sirf ek final yes/no nahi).
- Inner check expensive ya unsafe hai jab tak outer pass na ho (jaise: divide mat karo jab tak denominator check pass na ho).
HOW Python reads it — the indentation rule

Worked Example 1 — Login then role
logged_in = True
is_admin = False
if logged_in: # outer gate
if is_admin: # inner gate (sirf tab reach hoga jab logged_in ho)
print("Welcome, admin!")
else:
print("Welcome, user!")
else:
print("Please log in.")Trace:
logged_inhaiTrue→ outer body mein enter. Kyun? Outerelsebilkul skip ho jaata hai.is_adminhaiFalse→ innerelselo. Kyun? Inneriftak reach hua kyunki hum outer gate pass kar chuke the.- Output:
Welcome, user!
Worked Example 2 — Number sign and magnitude
n = -7
if n >= 0:
if n == 0:
print("zero")
else:
print("positive")
else:
if n < -10:
print("very negative")
else:
print("negative") # ← yahi run karegaTrace:
n >= 0?-7 >= 0haiFalse→ outerelsepar jump. Kyun? Pehli poori branch (uske innerifsamet) skip ho jaati hai.elseke andar:n < -10?-7 < -10haiFalse→ innerelse. Kyun? Ab hum negative case ke andar choose kar rahe hain.- Output:
negative
Worked Example 3 — Nesting vs flattening with and
# Nested
if age >= 18:
if has_license:
print("Can drive")
# Flattened — same result, kam depth
if age >= 18 and has_license:
print("Can drive")Forecast-then-Verify
Common Mistakes
Flashcards
Nested conditional kya hota hai?
if/elif/else jo doosre conditional ki body ke andar rakha jaata hai; inner tabhi run karta hai jab outer branch li gayi ho.Python mein, kya decide karta hai ki kaunsa if ek else se belong karta hai?
else nearest unmatched if se same indentation par pair karta hai.Nesting ko and mein flatten kab karna chahiye?
if False: ... else: if True: print("x") ka output?
x — outer False hai toh outer else lete hain, phir inner True x print karta hai.Inner condition kabhi kabhi check kyun nahi hoti?
if samet.if x = 5: kaunsa error raise karta hai aur kyun?
= assignment hai; comparison ke liye == chahiye.Recall Feynman: explain to a 12-year-old
Ek treasure box socho jo doosre box ke andar hai. Treasure paane ke liye tumhe pehle bada box kholna hoga. Sirf bada box khulne ke baad hi tum chhota box dekhte ho aur uska lock try karte ho. Agar bada box band raha, toh chhote ko kabhi haath nahi lagaya. Nested ifs bilkul wahi hai: andar wala sawaal sirf tab poocha jaata hai jab bahar wala sawaal "haan" keh de. Tum apni line ko daayein kitna push karte ho (indentation) yeh Python ko batata hai ki ek line kaunse box ke andar hai.
Connections
- Conditional statements (if-elif-else) — woh building block jo nest ho raha hai.
- Boolean logic and operators —
and/orkuch nests ko flatten kar sakte hain. - Indentation in Python — woh rule jo nesting ko unambiguous banata hai.
- Comparison operators —
==,<,>=conditions mein use hote hain. - Code readability and refactoring — deep "arrow code" se bachna.