You must monitor the flow of your program when writing code in any language. This is usually the case when there is decision-making involved: you’ll want to run one set of code if a condition is met, and a different set of code if it isn’t. The if, elif, and else statements in Python are useful for this.
We’ll learn how to use the if…elif…else declaration to change the flow of code in several directions based on a conditional expression in this article.
If..Elif…Else Statement in Python
1)Need of If..Elif..Else statement
For example, suppose we have a variable p and want our program to behave as follows:
If p is greater than 50 but less than 100, only statements 1 and 2 should be run.
If p is greater than 100 but less than 200, only statements 3 and 4 should be executed.
If p is greater than 200 but less than 300, only statements 5 and 6 should be executed.
If none of the above conditions apply to x, then only statements 7 and 8 should be executed.
According to the rationale above, only two statements should be executed for any value of p.
Now we want to monitor the execution flow in our software so that, depending on the value of x, code flow can be changed to one of four different directions.The if…elif…else declaration makes this easy.
2)Syntax of if..elif..else
if condition1: Statement_1 Statement_2 elif condition2: Statement_3 Statement_4 elif condition3: Statement_5 Statement_6 else: Statement_7 Statement_8
We can have any number of elif blocks after the if-block. However, we can only have one more block in the end. Just one of these code blocks will be executed. When the interpreter comes across an if…elif…else block, it,
- If the condition in the if-statement evaluates to True, the code in the if block is executed, and the remaining elif and else blocks are skipped.
- If the condition in the if-statement evaluates to False, the code in the if-block is skipped, and the condition in the first elif statement is evaluated, and if that evaluates to True, the code in the elif block is executed, and the remaining elif and else blocks are skipped.
- If the elif condition evaluates to False, the programme skips the elif block and moves on to the next. If the next block is also an elif, the process repeats until it either finds an else block or an elif block where the condition evaluates to True.
- If none of the if-requirements statement’s were met, and all elif statements returned False. Since there is no condition associated with the else block, the interpreter hops to the else-block and executes all of the statements in the else-block.
So, if any of the conditions in the if-statement and all of the elif-statements evaluate to False, the code in the else-block is executed.
3)Examples of if..elif..else
Example -1:
p = 56 if p < 100: print('If block executed') print('p is smaller than 100') elif 100 <= p < 200: print('first elif block is executed') print('p is between 100 and 200') elif 200 <= p < 300: print('second elif block is executed') print('p is between 200 and 300') else: print('Else block is executed') print('p is greater than 300')
Output:
If block executed p is smaller than 100
Explanation:
Since the value of p is 56, the if-statement condition evaluates to True. As a result, it only ran the code in the if-block, skipping all of the elif and else blocks.
Example-2:
p = 156 if p < 100: print('If block executed') print('p is smaller than 100') elif 100 <= p < 200: print('first elif block is executed') print('p is between 100 and 200') elif 200 <= p < 300: print('second elif block is executed') print('p is between 200 and 300') else: print('Else block is executed') print('p is greater than 300')
Output:
first elif block is executed p is between 100 and 200
Explanation:
Since x is 156, the if-statement condition evaluates to False, but the first elif-statement condition evaluates to True. As a result, the if-block was skipped and only the code in the first elif block was executed. The rest of the elif and else blocks are skipped.
Example-3:
p = 256 if p < 100: print('If block executed') print('p is smaller than 100') elif 100 <= p < 200: print('first elif block is executed') print('p is between 100 and 200') elif 200 <= p < 300: print('second elif block is executed') print('p is between 200 and 300') else: print('Else block is executed') print('p is greater than 300')
Output:
second elif block is executed p is between 200 and 300
Explanation:
Since x is 256, the if-block and first elif-block were skipped. In comparison, the code was only executed in the second elif-block. The rest of the elif and else blocks are skipped.
Example-4:
p = 469 if p < 100: print('If block executed') print('p is smaller than 100') elif 100 <= p < 200: print('first elif block is executed') print('p is between 100 and 200') elif 200 <= p < 300: print('second elif block is executed') print('p is between 200 and 300') else: print('Else block is executed') print('p is greater than 300')
Output:
Else block is executed p is greater than 300
Related Programs:
- python programming compound statement functions
- python programming compound statement built in functions
- python programming compound statement parameter
- python programming simple statement
- python programming compound statement
- python open a file using open with statement and benefits explained with examples
- python if elif else statement