Nested if-else Statement in Python

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.

Basic Structure

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

Example

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)}")

Explanation

  1. Outer if-else Statement:
    • The outer if-else checks if the score is greater than or equal to 60.
  2. Nested if Statement (First Branch):
    • If the score is greater than or equal to 60, it then checks if the score is greater than or equal to 85.
    • If true, it sets the performance to “Pass with Distinction”.
    • Otherwise, it sets the performance to “Pass”.
  3. Nested if-else Statement (Second Branch):
    • If the score is less than 60, it checks if the score is greater than or equal to 50.
    • If true, it sets the performance to “Fail but Eligible for Re-exam”.
    • Otherwise, it sets the performance to “Fail”.

More Complex Example: Age and Citizenship Check

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)}")

Explanation

  1. Outer if-else Statement:
    • The outer if-else checks if the age is 18 or older.
  2. Nested if Statement (First Branch):
    • If the age is 18 or older, it then checks if the person is a citizen.
    • If true, it sets the eligibility to “Eligible to vote”.
    • Otherwise, it sets the eligibility to “Not eligible to vote due to citizenship”.
  3. Nested if-else Statement (Second Branch):
    • If the age is less than 18, it checks if the age is 16 or older.
    • If true, it sets the eligibility to “Not eligible to vote but can pre-register”.
    • Otherwise, it sets the eligibility to “Not eligible to vote”.

Important Considerations

  • Readability: Nested 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.
  • Logical Complexity: Be careful with nested conditions, as they can introduce logical errors if not managed properly. Always test your code thoroughly.

Refactoring Nested if-else Statements

To 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.