Having trouble understanding "elif statement"

Hello Everyone,

I’m learning Python for the first time and am having trouble fully grasping the concept/purpose of the “elif” statement.
Can anyone explain how the statement functions with the if statement?

Thanks.

An elif (else if) statement can only follow an if statement. They can be chained together to control program flow.

We start with the condition of the if statement being checked. If it evaluates to True the specific code block related to it is executed. It then shortcuts any of the following elif and else statements, they are completely ignored.

Should it evaluate to False the following elif statement is checked. If that evaluates to True then it executes the code block attached to the elif statement and shortcuts any further elif or else statements. If it evaluates to False the chain moves onto the next expression and evalutes it.

So you have a chain of expressions that are evaluated one by one. If any single one evaluates to True its code block is executed and the rest of the chain is skipped.

An else statement can be added to the end of this chain and if each and every preceding statement evalutes to False then the code block associated with it is executed.

In the example below x is equal to integer 6. We first check the if statement. Is x less than zero? No it is is not. So we start moving down the chain. Is it between 0 and 5 inclusive, no. So we move to the second elif statement. That one is also False. So we move to the third elif statement. Is x grteater than 5 but less than or equal to 10. Yes.

We execute the code block associated with that elif: print(“third elif statement”). Since it evaluted to True we skip any further elif and else statements in the code entirely. Even the expressions aren’t executed.

x = 6

if x < 0:
    # if x is less than zero, do this...
    print("first if statement")
elif 0 <= x <= 5:
    # if x is greater than or equal to zero but less than or equal to 5, do...
    print("first elif statement")
elif 0 <= x <= 5:
    # if x is greater than or equal to zero but less than or equal to 5, do...
    print("second elif statement")
    # note that this one will never execute because the condition will be met
    # in the first elif statement.
elif 5 < x <= 10:
    # if x is greater than 5 but less than or equal to 10 do...
    print("third elif statement")
elif x == 12:
   # else if x is equal to twelve
    print("fourth elif statement")
else:
    # if no other condition is met then do this...
    print("all ELSE is False")

Try it with a few different values and see if you can get each of the print statements to execute (exlcuding the second elif statement unless you’re feeling highly adventurous).

5 Likes

@tgrtim explained it wonderfully.

A personal analogy I think of is when you are giving instructions to someone: get me a cheese sandwich from the store. They’ll reply what if they don’t have? And if they don’t have that? And if they don’t have anything?


order = cheese_sandwich
if order not in menu:
   order = ham
elif order not in menu:
   order = chips
else:
   cry()
4 Likes

elif is not a statement, it’s an optional part of an if-statement, same with else.

An if-statement amounts to jumps to different parts of the code based on conditions, elif is just more of the same thing.

1 Like

In a flow chart we use a rhombus (think ball diamond) to indicate conditional branching. The simplest form is if x.

if condition:
    # follow this branch
# end of if
# proceed from here

The above will follow the branch only when condition evaluates to True, as in, is truthy. Flow will continue down the code to the next statement, which will occur regardless of the outcome of if.

if dark:
    dim screen
print check_brightness

Above in pseudo-code form we see that the `check_brightness’ step is taken whether we dim the screen or not.

That is the simplest form of an if statement.

2 Likes