The nested if statement is an important concept in Python that allows you to place one if statement inside another if statement.
It is used when you need to check multiple conditions in a step-by-step manner.
🧠 What is a Nested if Statement?
A nested if statement means:
👉 An if inside another if
This helps you create more detailed decision-making logic in your program.
⚙️ Syntax of Nested if
if condition1:
if condition2:
# code runs if both conditions are True🧪 Basic Example
age = 20
if age >= 18:
if age >= 21:
print("Eligible for full privileges")
else:
print("Eligible but limited access")Output:
Eligible but limited access🔍 How Nested if Works
Python checks conditions step by step:
- First
ifcondition is checked - If True → move inside
- Second
ifcondition is checked - Based on result → execute final block
🎯 Real-Life Example: Login System
username = "admin"
password = "1234"
if username == "admin":
if password == "1234":
print("Login Successful")
else:
print("Incorrect Password")
else:
print("User not found")🏦 Real-World Example: Banking System
balance = 1000
withdraw = 500
if balance > 0:
if withdraw <= balance:
print("Transaction Successful")
else:
print("Insufficient Balance")
else:
print("No balance available")🎮 Game Level Example
level = 10
has_key = True
if level >= 5:
if has_key:
print("Next level unlocked")
else:
print("You need a key")🛒 Shopping Example
cart_total = 120
membership = True
if cart_total > 100:
if membership:
print("Extra discount applied")
else:
print("Standard discount applied")🔁 Nested if with Multiple Levels
You can nest multiple if statements.
age = 25
citizen = True
registered = True
if age >= 18:
if citizen:
if registered:
print("Eligible to vote")⚖️ Nested if vs if-elif-else
| Feature | Nested if | if-elif-else |
|---|---|---|
| Structure | Multiple layers | Single chain |
| Complexity | Higher | Lower |
| Use case | Detailed conditions | Simple decisions |
⚡ When to Use Nested if
Use nested if when:
✔ You need step-by-step checks
✔ One condition depends on another
✔ Multi-level validation is required
⚠️ Common Mistakes
Too deep nesting
❌ Hard to read
if a:
if b:
if c:
print("Too deep")✔ Better approach: simplify logic
Wrong indentation
if age > 18:
if has_id:
print("Allowed")🚀 Applications of Nested if
Nested if is used in:
- Login systems 🔐
- Banking applications 💰
- Game logic 🎮
- Access control systems 🛂
- AI decision layers 🤖
- Multi-step validation ✔️
💡 Best Practices
✔ Avoid deep nesting (keep it 2–3 levels max)
✔ Use meaningful variable names
✔ Break complex logic into functions
✔ Keep code readable
🧾 Conclusion
The nested if statement allows Python programs to handle complex decision-making by checking multiple conditions in a structured way.
It is very useful in real-world applications like authentication systems, games, and financial systems.
🎯 Final Thought
Nested if statements help your Python programs make smarter and more detailed decisions, but they should be used carefully to keep your code clean and readable.


0 Comments