Nested if Statements
A nested if statement is an if inside another if.
It is used to check conditions within conditions, making it useful for multi-level decision making.
The inner block executes only when the outer condition is True.
This type of statement is commonly used for handling complex logic in programs.
Explanation
- First, outer condition is checked
- If True, inner condition is checked
- If both are True, the code runs
- If outer is False, inner is skipped
- Used when decisions depend on each other
Real-Time Scenarios
- If login correct → then check password
- If ticket available → then check ID
- If exam passed → then check grade
- If ATM card valid → then check PIN
- If user registered → then allow access

Syntax
if condition1:
if condition2:
# block
Example 1
marks = 75
if marks >= 50:
if marks >= 70:
print("Good marks")
Output:
Good marks
Example 2
username = "admin"
password = "1234"
if username == "admin":
if password == "1234":
print("Login successful")
else:
print("Wrong password")
else:
print("Wrong username")
Output:
Login successful
Explanation
- Outer condition checks username
- Inner condition checks password
- Both must be True for successful login
Why Nested if is Important
- Helps handle multiple related conditions
- Improves logical control
- Used in real-world systems like login and security
- Makes programs more structured
Common Beginner Mistakes
- Too many nested levels
- Wrong indentation
- Missing conditions
- Not understanding execution flow
➤ Key Points
Nested ifmeans an if inside another if- Inner condition runs only if outer is True
- Used for complex conditions
- Helps in better decision making
➤ Tips
- Avoid too many nested levels (keep it simple)
- Use proper indentation
- Combine conditions using and / or when possible
- Practice with real-life examples