{"id":6336,"date":"2023-10-30T11:29:45","date_gmt":"2023-10-30T05:59:45","guid":{"rendered":"https:\/\/python-programs.com\/?p=6336"},"modified":"2023-11-10T12:07:31","modified_gmt":"2023-11-10T06:37:31","slug":"python-if-elif-else-statement","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-if-elif-else-statement\/","title":{"rendered":"Python \u2013 If..Elif\u2026Else Statement"},"content":{"rendered":"
You must monitor the flow of your program\u00a0when 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.<\/p>\n
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.<\/p>\n
For example, suppose we have a variable p\u00a0and want our program\u00a0to behave as follows:<\/p>\n
If p\u00a0is greater than 50 but less than 100, only statements 1 and 2 should be run.
\nIf p\u00a0is greater than 100 but less than 200, only statements 3 and 4 should be executed.
\nIf p\u00a0is greater than 200 but less than 300, only statements 5 and 6 should be executed.
\nIf none of the above conditions apply to x, then only statements 7 and 8 should be executed.
\nAccording to the rationale above, only two statements should be executed for any value of p.<\/p>\n
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.<\/p>\n
if condition1:\r\n Statement_1\r\n Statement_2\r\nelif condition2:\r\n Statement_3\r\n Statement_4\r\nelif condition3:\r\n Statement_5\r\n Statement_6\r\nelse:\r\n Statement_7\r\n Statement_8<\/pre>\nWe 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,<\/p>\n
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.<\/p>\n
Example -1:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n 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.<\/p>\n Example-2:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n 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.<\/p>\n Example-3:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n 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.<\/p>\n Example-4:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n You must monitor the flow of your program\u00a0when 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 …<\/p>\np = 56\r\nif p < 100:\r\n print('If block executed')\r\n print('p is smaller than 100')\r\nelif 100 <= p < 200:\r\n print('first elif block is executed')\r\n print('p is between 100 and 200')\r\nelif 200 <= p < 300:\r\n print('second elif block is executed')\r\n print('p is between 200 and 300')\r\nelse:\r\n print('Else block is executed')\r\n print('p is greater than 300')\r\n<\/pre>\n
If block executed\r\np is smaller than 100<\/pre>\n
p = 156\r\nif p < 100:\r\n print('If block executed')\r\n print('p is smaller than 100')\r\nelif 100 <= p < 200:\r\n print('first elif block is executed')\r\n print('p is between 100 and 200')\r\nelif 200 <= p < 300:\r\n print('second elif block is executed')\r\n print('p is between 200 and 300')\r\nelse:\r\n print('Else block is executed')\r\n print('p is greater than 300')\r\n<\/pre>\n
first elif block is executed\r\np is between 100 and 200<\/pre>\n
p = 256\r\nif p < 100:\r\n print('If block executed')\r\n print('p is smaller than 100')\r\nelif 100 <= p < 200:\r\n print('first elif block is executed')\r\n print('p is between 100 and 200')\r\nelif 200 <= p < 300:\r\n print('second elif block is executed')\r\n print('p is between 200 and 300')\r\nelse:\r\n print('Else block is executed')\r\n print('p is greater than 300')\r\n<\/pre>\n
second elif block is executed\r\np is between 200 and 300<\/pre>\n
p = 469\r\nif p < 100:\r\n print('If block executed')\r\n print('p is smaller than 100')\r\nelif 100 <= p < 200:\r\n print('first elif block is executed')\r\n print('p is between 100 and 200')\r\nelif 200 <= p < 300:\r\n print('second elif block is executed')\r\n print('p is between 200 and 300')\r\nelse:\r\n print('Else block is executed')\r\n print('p is greater than 300')\r\n<\/pre>\n
Else block is executed\r\np is greater than 300<\/pre>\n
\n