Control Flow: The if, elif, else Statements

Multiple elif Statements

The elif condition checks are performed after one another if the initial if check fails to evaluate to True.  

if (Condition1):
    execute this code block if Condition1 evaluates to True
    
elif (Condition2):
    execute this code block if Condition1 evaluates to False and Condition2 evaluates to True
    
elif (Condition3):
    execute this code block if Condition1 and Condition2 evaluate to False and Condition3 evaluates to True
    
elif (Condition4):
    execute this code block if Condition1, Condition2, and Condition3 evaluate to False and Condition4 evaluates to True
    
else:
    execute this code block if none of the conditions above evaluates to True 
y = int ( input("Enter a number please: ") )
x = y + 3

print("x =", x)

if (x <= 10) :
    print("x is less than or equal to 10")
    remainder = x % 10

elif (x < 20) :
    print("x is greater than 10 and less than 20")
    remainder = x % 5

elif (x < 30) :
    print("x is greater than or equal to 20 and less than 30")
    remainder = x % 5

else:
    print("x is greater than or equal to 30")
    remainder = x % 2

print("remainder =", remainder)

Conditional blocks can be nested inside other conditional blocks as well. It is possible to add other if-elif-else blocks inside other if, elif, or else blocks.

if (Condition1):
    execute this code block if Condition1 evaluates to True
    
    if(Condition1_1_0):
        execute this code block if Condition1 and Condition1_1_0 evaluate to True
    elif(Condition1_1_1):         
        execute this code block if Condition1 evaluates to True, Condition1_1_0 evaluates to False, and Condition1_1_1 evaluates to True
    elif(Condition1_1_2):         
        execute this code block if Condition1 evaluates to True, Condition1_1_0 and Condition1_1_1 evaluate to False, and Condition1_1_2 evaluates to True
    # not stating an else statement in this section

    # starting a new nested if condition sequence independent of the one started as if(Condition1_1_0):
    if(Condition1_2_0):
        execute this code block if Condition1 and Condition1_2_0 evaluate to True
    elif(Condition1_2_1):         
        execute this code block if Condition1 evaluates to True, Condition1_2_0 evaluates to False, and Condition1_2_1 evaluates to True
    else:         
        execute this code block if Condition1 evaluates to True and none of the Condition1_2_0 and Condition1_2_1 evaluates to True
    
elif (Condition2):
    execute this code block if Condition1 evaluates to False and Condition2 evaluates to True
    if(Condition2_1_0):
        execute this code block if Condition1 evaluates to False, Condition2 and Condition2_1_0 evaluate to True
    elif(Condition2_1_1):         
        execute this code block if Condition1 evaluates to False, Condition2 evaluates to True, Condition2_1_0 evaluates to False, and Condition2_1_1 evaluates to True
    else:         
        execute this code block if Condition1 evaluates to False, Condition2 evaluates to True, and none of the Condition2_1_0 and Condition2_1_1 evaluate to True
        
elif (Condition3):
    execute this code block if Condition1 and Condition2 evaluate to False and Condition3 evaluates to True
    
elif (Condition4):
    execute this code block if Condition1, Condition2, and Condition3 evaluate to False and Condition4 evaluates to True
    
else:
    execute this code block if none of Condition1, Condition2, Condition3, and Condition4 evaluates to True