Table of Contents
What is a Nested if Statement in Python?
A nested if statement is a conditional statement inside another conditional statement. Python first evaluates the outer if condition. If it is True, Python enters the outer block and then evaluates the inner if condition.If the outer condition is False, Python skips all the nested conditions inside it.
Syntax:
You can also combine if, elif, and else inside nested blocks.if condition1:
# Executes if condition1 is True
if condition2:
# Executes if both condition1 and condition2 are True
Flowchart of Nested if Statement
The flowchart illustrates how a nested if statement evaluates multiple conditions before deciding which block of code to execute.
The execution process is explained below.
Step 1: Start the Program: The program begins execution and reaches the first if statement.
Step 2: Evaluate the First Condition: Python checks Condition 1. This is the outer if condition that determines whether the nested if statement should be evaluated.
Step 3: Check the Result of Condition 1:
- If Condition 1 is True, Python enters the outer if block and evaluates the nested condition.
- If Condition 1 is False, Python skips the entire outer if block and executes the outer else block (if present).
Step 4: Evaluate the Nested Condition: If the first condition is True, Python evaluates Condition 2, which is the condition inside the nested if statement.
Step 5: Execute the Appropriate Block: Based on the result of Condition 2:
- If Condition 2 is True, Python executes the statements inside the inner if block.
- If Condition 2 is False, Python executes the statements inside the inner else block.
Step 6: Continue Program Execution: After executing either the inner if, inner else, or outer else block, Python exits the nested if structure and continues executing the remaining statements in the program.
Examples of Nested If
Example 1:
# Python program to demonstrate nested if statement
marks = 87
if marks >= 35:
print("Student Passed")
if marks >= 75:
print("with Distinction")
Output:
Explanation:Student Passed
with Distinction
The student first satisfies the passing condition. Since the program enters the inner block, it checks the second condition for distinction, which is also true.
Example 2: Multiple Levels of Nested if
# Python program to demonstrate Multiple Levels
# of Nested if
number = 24
if number > 0:
if number % 2 == 0:
if number % 4 == 0:
print("Positive, Even and Divisible by 4")
else:
print("Positive and Even")
else:
print("Positive but Odd")
else:
print("Negative Number")
Output:
Explanation:Positive, Even and Divisible by 4
The program checks three conditions one after another:
- Whether the number is positive.
- Whether it is even.
- Whether it is divisible by 4.
Each condition depends on the previous one being true.
Conclusion
A nested if statement in Python is an if statement placed inside another conditional statement. It enables programs to evaluate multiple dependent conditions in a structured way. Nested if statements are particularly useful in applications such as login authentication, banking systems, admission processes, and employee management systems where one decision depends on the result of another. Although nested conditions make programs more powerful, excessive nesting can reduce readability and increase complexity. Following proper indentation, keeping conditions simple, and avoiding unnecessary nesting help create clean, efficient, and maintainable Python code.
Frequently Asked Questions
1. When should I use a nested if statement?
2. Can I nest multiple if statements in Python?Use a nested if statement when one condition should be checked only after another condition has already been satisfied.
3. How can I reduce deep nesting in Python?Yes. Python allows multiple levels of nesting. However, excessive nesting should be avoided because it reduces code readability.
4. What is the difference between a nested if statement and using the and operator?You can reduce deep nesting by using logical operators (and, or), early returns in functions, helper functions, or restructuring your conditions to make the code cleaner and easier to maintain.
5. Can I use nested if statements inside loops in Python?A nested if statement checks conditions one after another, where the inner condition is evaluated only if the outer condition is true. The and operator combines multiple conditions into a single if statement. Nested if statements are more suitable when each condition requires different actions or messages.
Yes. Nested if statements can be used inside for and while loops to perform conditional checks during each iteration. This is commonly used when processing lists, validating input, filtering data, or searching for specific values.
0 Comments