Zach

Blog

Python IF-statement vs ELIF-statement

5-18-2019

I had a moment of clarity recently about the functional difference between the Python IF-statement and ELIF-statement. I’ve used these numerous times before to accomplish what I wanted, but this was one of those moments where you learn something you thought you already knew.

Functional Summary

During execution, an IF-statement is testing that the defined conditions are met; if met the code under the IF-statement is triggered. Take note that, if you have multiple IF-statements, modifying the same variable, you need to keep in mind that multiple statements might execute and could modify the same variable (unexpectedly).

Here is where the ELIF-statement differs:

The elif, also known as else if in other languages, still tests whether the defined conditions are met, but as soon as the first ELIF-statement that meets the defined conditions is found it executes that ELIF-statement and NO other ELIF-statement conditions run or are even looked at because one of the ELIF-statements were found to be True. Meaning, if you have three ELIF-statements and the first one is executed the other two are ignored.

To summarize, IF-statements go through every single statement to see if the criteria is met, but ELIF-statements go until the first instance of the criteria is met and then leaves the block and stops checking all other ELIF-statements.

Note: This behavior can make an enormous impact on how your software logic works, so keep this in mind when designing your software.

IF-ELSE statement Example

To start things out let’s look at one of the most basic forms of an IF-statement, the IF-ELSE statement. The below code checks to see if you exceed the 70mph speed limit, if you do it will print “Gotcha”, otherwise (else) it will print “Nothing to see here”.

speed_limit = 70

if speed_limit > 70:
    result = "Gotcha!"
    print (result)
else:
    result = "Nothing to see here"
    print (result)


Ok, now let's get to some slightly more complex examples.

IF-statement Examples

This example has a speed_limit set to 70mph. The IF-statements are checking to see if you are under, meet, or exceed the speed limit. Based on the logic checks it prints out a result.

Now let's show the code to give you a visual representation:

speed_limit = 70

if speed_limit > 70:
    result = "Gotcha!"
    print (result)
if speed_limit < 70:
    result = "Nothing to see here"
    print (result)
if speed_limit == 70:
    result = "You like to push the limits"
    print (result)
if speed_limit == 70:
    result = "Just double checking"
    print (result)


Which will print the following to the terminal:

You like to push the limits
Just double checking


The above results are shown because both of the IF-statements that check to see if the speed_limit equals 70mph execute.

To frame this situation in another way, let’s see what happens when multiple IF-statements are executed, changing the same variable, and the result only prints to the terminal at the end of the logic checks. What we are seeing here is how the interaction of the two IF-statements that trigger effect the result variable. The last statement to trigger will be the value set to result.

speed_limit = 70

if speed_limit > 70:
    result = "Gotcha!"
if speed_limit < 70:
    result = "Nothing to see here"
if speed_limit == 70:
    result = "You like to push the limits"
if speed_limit == 70:
    result = "Just double checking"
print (result)


Which will print the following to the terminal:

Just double checking


The above only prints the last IF-statement value, so you better be sure this is the behavior you want when designing your logic.

ELIF-statement Example

This example has a speed_limit set to 70mph. The IF and ELIF-statements are checking to see if you are under, meet, or exceed the speed limit. Based on the results it prints out a single result.

speed_limit = 70

if speed_limit > 70:
    result = "Gotcha!"
    print (result)
elif speed_limit < 70:
    result = "Nothing to see here"
    print (result)
elif speed_limit == 70:
    result = "You like to push the limits"
    print (result)
elif speed_limit == 70:
    result = "Just double checking"
    print (result)


Which will print the following to the terminal:

You like to push the limits


The above result shows because only the first condition that evaluates true executes, and once complete the IF-ELIF block exits. This is why the second ELIF-statement is NOT printed to the terminal.