A nested if statement in Python is when an if statement is placed inside another if or else block. This allows you to check additional conditions after a previous condition has been evaluated. Nested if statements are useful when you need to make multiple levels of decision-making.
Syntax of Nested if Statement:
if condition1:
if condition2:
# Code block to execute if condition1 and condition2 are True
else:
# Code block to execute if condition1 is True but condition2 is False
else:
# Code block to execute if condition1 is False
The outer if checks the first condition.
If the first condition is True, the inner if checks the second condition.
The else part of the inner if is executed if the inner condition is False.
If the outer if is False, the else block for the outer if is executed.
Example 1: Basic Nested if Statement
x = 10
y = 5
if x > 5:
if y < 10:
print("x is greater than 5 and y is less than 10")
else:
print("x is greater than 5 but y is not less than 10")
else:
print("x is not greater than 5")
Output: x is greater than 5 and y is less than 10
In this example:
The outer if condition x > 5 is True, so it checks the inner if condition y < 10.
Since y < 10 is True, the code inside the inner if block is executed.
Example 2: Nested if with Multiple Conditions
x = 8
y = 15
if x > 5:
if y > 10:
print("x is greater than 5 and y is greater than 10")
else:
print("x is greater than 5 but y is not greater than 10")
else:
print("x is not greater than 5")
Output: x is greater than 5 and y is greater than 10
Here:
The outer if condition x > 5 is True.
The inner if condition y > 10 is also True, so the inner block is executed.
Example 3: Nested if with else in Both Layers
x = 4
y = 20
if x > 5:
if y > 10:
print("x is greater than 5 and y is greater than 10")
else:
print("x is greater than 5 but y is not greater than 10")
else:
if y > 10:
print("x is not greater than 5 but y is greater than 10")
else:
print("x is not greater than 5 and y is not greater than 10")
Output: x is not greater than 5 but y is greater than 10
In this case:
The outer if condition x > 5 is False, so it checks the else part of the outer if.
The inner if condition y > 10 is True, so the code inside the inner block of the else is executed.
Example 4: Multiple Levels of Nesting
x = 7
y = 10
z = 5
if x > 5:
if y > 8:
if z > 3:
print("x is greater than 5, y is greater than 8, and z is greater than 3")
else:
print("x is greater than 5, y is greater than 8, but z is not greater than 3")
else:
print("x is greater than 5 but y is not greater than 8")
else:
print("x is not greater than 5")
Output: x is greater than 5, y is greater than 8, and z is greater than 3
Here:
The outer if condition x > 5 is True.
The inner if condition y > 8 is also True.
The innermost if condition z > 3 is True, so the final block is executed.
Key Points:
Nested if statements allow for more complex decision-making by evaluating conditions inside other conditions.
It’s important to manage the indentation properly, as it determines the structure of nested conditions.
You can nest multiple if statements, but too many levels of nesting can make the code harder to read. In such cases, consider using logical operators (and, or, not) or breaking the logic into separate functions for better readability.
Good Practice:
Avoid excessive nesting, as it can lead to complex, hard-to-maintain code. If you find yourself nesting too deeply, it might be a sign to refactor the code.
Let me know if you need further clarification or examples! 😊
0 Comments