A nested if-else
statement in Python is an if-else
statement inside another if-else
statement. This allows you to check multiple conditions and make more complex decisions in your code. Nested if-else
statements are useful when the outcome of one condition depends on another.
The basic structure of a nested if-else
statement is as follows:
if condition1:
# Block of code if condition1 is true
if condition2:
# Block of code if condition2 is also true
else:
# Block of code if condition2 is false
else:
# Block of code if condition1 is false
Let’s consider an example where we determine a student’s performance based on their score, and if they pass, we also check if they have a distinction.
def determine_performance(score):
if score >= 60:
if score >= 85:
performance = "Pass with Distinction"
else:
performance = "Pass"
else:
if score >= 50:
performance = "Fail but Eligible for Re-exam"
else:
performance = "Fail"
return performance
# Example usage
score = 78
print(f"Performance for score {score}: {determine_performance(score)}")
score = 45
print(f"Performance for score {score}: {determine_performance(score)}")
if-else
Statement:if-else
checks if the score is greater than or equal to 60.if
Statement (First Branch):if-else
Statement (Second Branch):Here’s another example where we check a person’s eligibility to vote based on age and citizenship status:
def check_voting_eligibility(age, citizenship):
if age >= 18:
if citizenship == 'citizen':
eligibility = "Eligible to vote"
else:
eligibility = "Not eligible to vote due to citizenship"
else:
if age >= 16:
eligibility = "Not eligible to vote but can pre-register"
else:
eligibility = "Not eligible to vote"
return eligibility
# Example usage
age = 20
citizenship = 'citizen'
print(f"Eligibility for age {age} and citizenship {citizenship}: {check_voting_eligibility(age, citizenship)}")
age = 15
citizenship = 'citizen'
print(f"Eligibility for age {age} and citizenship {citizenship}: {check_voting_eligibility(age, citizenship)}")
age = 17
citizenship = 'resident'
print(f"Eligibility for age {age} and citizenship {citizenship}: {check_voting_eligibility(age, citizenship)}")
if-else
Statement:if-else
checks if the age is 18 or older.if
Statement (First Branch):if-else
Statement (Second Branch):if-else
statements can quickly become difficult to read and maintain. It is often a good idea to refactor complex nested logic into functions or use other control structures where appropriate.if-else
StatementsTo improve readability and maintainability, you can sometimes refactor nested if-else
statements into functions or use logical operators:
def determine_performance(score):
if score >= 60:
return "Pass with Distinction" if score >= 85 else "Pass"
else:
return "Fail but Eligible for Re-exam" if score >= 50 else "Fail"
def check_voting_eligibility(age, citizenship):
if age >= 18:
return "Eligible to vote" if citizenship == 'citizen' else "Not eligible to vote due to citizenship"
elif age >= 16:
return "Not eligible to vote but can pre-register"
else:
return "Not eligible to vote"
By refactoring, the code remains clear and concise, making it easier to understand and maintain.