In this article, we will learn how to alter the flow of code in multiple directions based on the conditional expression using the if…elif…else statement.


In the previous two articles, we discussed if-statement and if…else statements. Using them, we can logically control the flow of execution in the program. We can direct the flow in any one of the two directions, i.e., either execute if block statements or else block statements based on the condition. But what if we want a possibility of multiple directions?

For example, we have a variable x, and we want the following behavior in our program,

  • If x is greater than 10, but lesser then 20, execute statement 1 & statement 2 only
  • If x is greater than 20 but lesser then 30, execute statement 3 & statement 4 only
  • If x is greater than 30 but lesser then 40,execute statement 5 & statement 6 only
  • If none of the above condition satisfies for x, then execute statement 7 & statement 8 only

Based on the above logic, for any value of x, only two statements should execute.

Now here we want to control the flow of execution in our program in such a way that based on the value of x, code flow can be altered in any one of the four different paths. We can do this easily with the if..elif…else statement.

Syntax of if..elif…else

If condition_1:
    Statement 1
    Statement 2
elif condition_2:
    Statement 3
    Statement 4
elif condition_3:
    Statement 5
    Statement 6
else:
    Statement 7
    Statement 8

Here, after the if-block, we can have any number of elif blocks. But we can have only one else block in the end. Only one of these blocks of code will execute. When the interpreter encounters an if…elif…else blocks then it,

  • First evaluates the condition in if-statement, and if that evaluates to True, then it executes the code in if block and skips all the remaining elif and else blocks.
  • If the condition in if-statement evaluates to False, then it skips the code in if-block and evaluates the condition in the first elif statement, and if that evaluates to True, then it executes the code in elif block and skips all the remaining elif and else blocks.
  • If the condition in elif evaluates to False, then it skips that elif block and jumps to the next block. Now, if the next block is also an elif then it repeats the same steps until, it finds a elif block where condition evaluates to True or, it finds an else block.
  • If none of the conditions in if-statement and all elif statements evaluated to False. Then the interpreter jumps to the else-block and executes all the statements in the else-block, because there is no condition associated with the else block.

So, else-block is a block whose code gets executed if all the conditions in if-statement and all elif-statements evaluates to False. Let’s see some example of if…elif…else

Examples of if…elif…else in Python

Example 1:

x = 2

if x  < 3:
    print('The if-block')
    print('x is smaller than 3')
elif 3 < x < 10:
    print('First elif block')
    print('x is between 3 & 10')
elif 10 < x < 20:
    print('Second elif block')
    print('x is between 10 & 20')
else:
    print('The else block')
    print('x is greater than 20')

Output:

The if-block
x is smaller than 3

The value of x is 3, so the condition in if-statement evaluates to True. Therefore it executed the code in if-block only and skipped all the remaining elif and else blocks.

Example 2:

x = 5

if x  < 3:
    print('The if-block')
    print('x is smaller than 3')
elif 3 < x < 10:
    print('First elif block')
    print('x is between 3 & 10')
elif 10 < x < 20:
    print('Second elif block')
    print('x is between 10 & 20')
else:
    print('The else block')
    print('x is greater than 20')

Output:

First elif block
x is between 3 & 10

The value of x is 5, so the condition in if-statement evaluates to False, but the condition in the first elif-statement evaluates to True. So it skipped the if-block and executed the code in the first elif block only. All the remaining elif and else blocks get skipped.

Example 3:

x = 16

if x  < 3:
    print('The if-block')
    print('x is smaller than 3')
elif 3 < x < 10:
    print('First elif block')
    print('x is between 3 & 10')
elif 10 < x < 20:
    print('Second elif block')
    print('x is between 10 & 20')
else:
    print('The else block')
    print('x is greater than 20')

Output:

Second elif block
x is between 10 & 20

As the value of x is 16, so it skipped the if-block and first elif-block. In contrast, executed the code in second elif-block only. All the remaining elif and else blocks get skipped.

Example 4:

x = 36

if x  < 3:
    print('The if-block')
    print('x is smaller than 3')
elif 3 < x < 10:
    print('First elif block')
    print('x is between 3 & 10')
elif 10 < x < 20:
    print('Second elif block')
    print('x is between 10 & 20')
else:
    print('The else block')
    print('x is greater than 20')

Output:

The else block
x is greater than 20

As the value of x is 36, so it skipped the if-block and all the elif-blocks. In contrast, executed the code the else block.

Conclusion:

So, this is how we can use the if…elif…else statement in python to write a code in which the flow of execution can be controlled and directed to any one of the multiple directions, based on numerous conditions.

Join a list of 2000+ Programmers for latest Tips & Tutorials