Nested if Statements
A Nested If Statement is a conditional statement where one if statement is written inside another if statement.
The inner block executes only when the outer condition is True.
This type of statement is commonly used for handling complex logic in programs.
What Does Nested Mean
The word:
Nested
means:
One thing inside another thing
Similarly, in Python:
- one
ifstatement inside anotherifstatement - is called a Nested If Statement
Nested If Statements in Python
Nested if statements are an important concept in Python programming used for checking conditions inside another condition.
They help programs make more detailed and complex decisions.
In real-world applications, many situations require checking one condition first and then checking another condition inside it.
For example:
- First check whether the username is correct
- Then check whether the password is correct
This type of logic is implemented using Nested If Statements.
Why Nested If Statements are Important
Nested if statements are important because they help programs:
- check multiple levels of conditions
- improve security systems
- create complex decision-making
- validate user information
- build real-world applications
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 Applications of Nested If Statements
| Application | Usage |
|---|---|
| Banking System | PIN verification and balance checking |
| Login Systems | Username and password validation |
| School Management | Admission eligibility |
| Online Shopping | Coupon and payment verification |
| Gaming Applications | Unlocking advanced levels |

Syntax
if condition1:
if condition2:
statements
Explanation of Syntax
| Part | Meaning |
|---|---|
condition1 |
Outer condition |
condition2 |
Inner condition |
statements |
Executes when both conditions are True |
How Nested If Statement Works
Step 1
Python first checks the outer condition.
Step 2
If the outer condition is True:
- Python moves to the inner if statement
- then checks the inner condition
Step 3
If the outer condition is False:
- Python skips the inner if statement completely
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 is True
- Inner condition is False
- So
"Hello"is skipped "Hi"executes because it is outside the nested block
Important Point
Proper indentation is very important in nested if statements.
Python uses indentation to identify:
- outer block
- inner block
Proper indentation is very important in nested if statements.
Python uses indentation to identify:
- outer block
- inner block
Common Beginner Mistakes
| Mistake | Problem |
|---|---|
| Wrong indentation | Indentation error |
Forgetting colon : |
Syntax error |
Using = instead of == |
Logical error |
| Incorrect nesting | Unexpected output |
➤ Summary
Nested if statements in Python are used when one condition depends on another condition.
Important Points
- One if statement inside another if statement
- Python first checks the outer condition
- Then Python checks the inner condition
- Indentation is very important in nested if statements
Nested if statements are widely used in:
-
- login systems
- banking applications
- school admission systems
- gaming applications
- security systems