After completing this chapter, you will be able to:
- Understand the Nested
ifstatement. - Explain why Nested
ifstatements are used. - Write programs using Nested
if. - Understand the execution flow of Nested
if. - Solve real-world problems involving multiple dependent conditions.
Sometimes, a single condition is not enough to make a decision.
A program may need to check another condition only if the first condition is True.
In such situations, Python provides the Nested if Statement.
A Nested if means placing one if statement inside another if statement.
A Nested if Statement is an if statement placed inside another if statement.
The inner if statement is executed only when the outer if condition evaluates to True.
Nested if statements are used when one condition depends on another condition.
- ATM Transaction
- Online Banking
- Employee Verification
- Student Scholarship Eligibility
- Login Authentication
- College Admission
if condition1:
if condition2:
statements Condition 1
/ \
True False
|
Condition 2
/ \
True False
|
Execute Block
|
Next Statement
- Python checks the outer
ifcondition. - If it is False, the inner
ifstatement is skipped. - If it is True, Python checks the inner
ifcondition. - If the inner condition is True, the nested block executes.
- Control moves to the next statement after the Nested
if.
Write a Python program to check whether a student is eligible for a scholarship.
Conditions:
- Marks should be greater than or equal to 75.
- Attendance should be greater than or equal to 80%.
# Student details
marks = 82
attendance = 90
# Check scholarship eligibility
if marks >= 75:
if attendance >= 80:
print("Eligible for Scholarship")Eligible for Scholarship
START
Create variable marks
Create variable attendance
Check marks >= 75
If True
Check attendance >= 80
If True
Display "Eligible for Scholarship"
STOP
marks = 82Stores the student's marks.
attendance = 90Stores the student's attendance percentage.
if marks >= 75:Checks whether the student has scored at least 75 marks.
if attendance >= 80:This condition is checked only if the first condition is True.
print("Eligible for Scholarship")Displays the eligibility message.
Write a Python program to verify the ATM PIN and account balance before allowing cash withdrawal.
pin = 1234
balance = 15000
if pin == 1234:
if balance >= 5000:
print("Transaction Successful")Transaction Successful
Write a Python program to determine whether an employee is eligible for a bonus.
Conditions:
- Experience must be at least 5 years.
- Performance rating must be at least 4.
experience = 6
rating = 4
if experience >= 5:
if rating >= 4:
print("Bonus Approved")Bonus Approved
Nested if statements are used in:
- Banking Applications
- Login Authentication
- College Admission Systems
- Scholarship Verification
- Employee Promotion Systems
- Online Examination Systems
- Handles dependent conditions efficiently.
- Improves logical decision-making.
- Suitable for complex validation.
- Easy to implement for multiple-level checks.
| if Statement | Nested if Statement |
|---|---|
| Checks a single condition. | Checks multiple dependent conditions. |
| Simple decision-making. | Multi-level decision-making. |
| One condition is evaluated. | One condition is evaluated inside another. |
❌ Incorrect
if marks >= 75:
if attendance >= 80:
print("Eligible")✅ Correct
if marks >= 75:
if attendance >= 80:
print("Eligible")Avoid using Nested if statements when a logical operator (and, or) can solve the problem more simply.
Example:
Instead of
if marks >= 75:
if attendance >= 80:
print("Eligible")You can also write
if marks >= 75 and attendance >= 80:
print("Eligible")Choose the approach that makes the program easier to understand.
A Nested if statement is an if statement placed inside another if statement. It is used to evaluate dependent conditions.
Nested if should be used when one condition depends on another condition being true.
Yes. Python allows multiple levels of nesting, but excessive nesting should be avoided to maintain readability.
Deeply nested code becomes difficult to read, understand, debug, and maintain.
Yes. In many situations, logical operators such as and and or can simplify the code.
- Nested
ifmeans placing oneifinside anotherif. - The inner
ifexecutes only when the outer condition isTrue. - Used for dependent conditions.
- Indentation is mandatory.
- Avoid unnecessary nesting when simpler alternatives exist.
In this chapter, you learned:
- What a Nested
ifstatement is. - Why it is used.
- Syntax and execution flow.
- Practical programs.
- Flow diagram.
- Real-world applications.
- Advantages.
- Common mistakes.
- Interview questions.