Introduction
Another article about control flow in Python programming language. There is already two articles before which has the same content. But those articles has a different focus because of the different format of reserved keyword used. In two previous article before, it is focusing not on if elif else reserved keyword. In the first previous article with the title of ‘How to use if as a control flow in Python’ in this link, it focus on ‘if’ reserved keyword. In the second exist in this link with the title of ‘How to Use if else as a control flow in Python’, it focus on ‘if else’ reserved keyword. But in this article, the focus will be the ‘if elif else’ reserved keyword for performing control flow.
How to use if elif else as a control flow in python
So, this part will focus on how to use the reserved keyword of ‘if elif else’ start from the syntax. In case of ‘if elif else’, the syntax in Python programming language exist as follows :
if conditional_statement : statement; elif conditional_statement : statement; else : statement;
Using the above syntax, the following is the pseudo code adopting the above syntax in order to design the source code in Python programming language. So, the pseudo code for designing the scenario in the Python programming language exist as follows :
if check_credit_score_equal_or_less_than 800 : print_to_the_output_device_credit_score_is_bad else if check_credit_score_greater_than 200 and check_credit_score_less_or_equal_than 400: print_to_the_output_device_credit_score_is_fair else if check_credit_score_greater_than 400 and check_credit_score_less_or_equal_than 600: print_to_the_output_device_credit_score_is_good else: print_to_the_output_device_credit_score_is_excellent
The above pseudo code actually have an additional ‘else if’. Basically, there can be more than one ‘else if’ between ‘if’ and ‘else’. Depends on the number of conditional statements available, it can also affect the ‘else if’ number. So, implementing the above pseudo code into a Python programming language’s source code along the execution of it in the Python command console, below is the execution of it :
>>> credit_score = 600; >>> if credit_score <= 200: ... print('Credit Score Bad'); ... elif credit_score > 200 and credit_score <=400: ... print('Credit Score Fair'); ... elif credit_score > 400 and credit_score <= 600: ... print('Credit Score Good'); ... else: ... print('Credit Score Excellent'); ... Credit Score Good >>>
The above is an example for assessing the score of the credit for normal loan application. Since it is just an example, the range is not repsenting the actual result in the real-life. So, the range for the credit score can result in a totally different classification between excellent, good, fair or bad. Furthermore, it can also have another different classification for the credit score. But the main purpose is just to show the usage of the if elif else reserved keyword to perform the control flow. It has four alternate process where the execution depends on the result of the conditional statement assessment in each part. Those parts are the conditional statement in if block, elif block and else block. If there is any conditional statement in that part met the condition, it will directly execute the statement exist inside that part.