If-elif-else statement in Python Programming
If-elif-else Statement in Python: If-elif-else statement is also the most important and easiest concept of control flow statements. This statement is very helpful, while or when we need to make the decision based on our problem what to do next.
The same type of problem arises sometimes in our programming also where we have to execute our code based on these decision making statements.
Let’s suppose we take an example: We have three numbers 1, 2 & 3, now we have said to write a program to check which number is greater, Now, in that case, we can easily find the greatest of three number(Greatest of three number) using if-elif statements.
But here we are talking about the if-elif-else statement. As if-elif-else is also used for multiple conditions, as discussed before that if all the conditions remain False then the if-elif statement does not return any output or result but if-elif-else may able to return some output/result. That’s the only difference between if-elif and if-elif-else statements.
We will learn about these flow control statements in detail.
Also, there are other control statements, These are given below:-
- if (already discussed)
- if-else (already discussed)
- if-elif (already discussed)
- if-elif-else
- nested-if
Let’s discuss all above statements in detail one by one:- If, if-else and if-elif statements we have discussed already, followed by this article (if-statement, if-else statement,if-elif statement), now we are discussing if-elif-else:
(i). If-elif-else statement: The execution of the if-elif-else statement is different from our previous control statements that we have discussed so far. If-elif-else statement returns the given result or output depending upon the input whether the given input is True or False. Especially, when we have to check multiple conditions then we prefer to use if-elif-else statements.
Note: Point to be noted is that, in if-elif statements, if the first condition is True it execute the block of code inside the True condition & if the first one condition is False and the second one is True then it will execute the block of code inside the second condition, But what if both of the condition is not True. “Yes” in that case it will execute the else block and return result or output. Same as the if-else statement.
For reference, you can see the below-given flow chart.
Syntax:
#if-elif-else statement example x = 21 if(x==22): print("True") elif(x==21): print("False") else: print("Yes") # output # False
The above given example is the easiest example to understand the if-elif-else statement. The only difference between the if-elif and if-elif-else statement is that the if-elif statement only shows the output as True if the condition is True & False if the condition is False. But if-elif-else returns output whether the input/condition is False. I hope this statement has been clear by the above given example. If you still have doubt, please refer to above given example again:
Example:
#if-elif-else statement example x = 18 if(x==23): print("True") elif(x==22): print("False") else: print("Yes") # output # Yes
It will display the result as Yes.
Other Control Flow Statements: