This is indeed another article which is still a part of the article describing about how to perform branching to control logic flow in programming language is here. It is also the continuation of another article with a similar content. It is an article with the title of ‘How to perform branching to control logic flow in python programming language using nested if elif statement’ in this link. In this article, the branching process controlling the logic flow of the execution of the process’s definition is using the if elif else statement. It has several alternative flows upon the execution of the process. Setting the logic of the execution is possible in order to control the flow of the process in the if elif else statement.
In python programming language, branching process feature or concept is available using several syntaxes. The syntax for branching the process in this article is using the if elif else statement. The term if elif means that if the condition fail in the if statement, it will directly try to execute the condition in the elif statement. In other words, there will be another if statement in the elif statement.
How many if and elif statement available ?. Actually, it depends on the definition of the condition. But in this context, after the execution of one block of if statement is a success, it will then finish the execution of the process. Eventually, in the if elif else statement, there is another additional control logic flow available. If there are no single condition in the if and elif statement success, it will stop in the else statement. In this context, the process will execute the else statement if the other blocks fail the condition.
Branching Example using If Else Statement
Fulfilling the condition is also a necessary in the if elif else statement. There are more than one if statement, so there are several branching process execution. Furthermore, there will be only one process execution at maximum. Even if it fail to meet all of the conditions, there will be only one execution in the else statement block. So, the following are the syntaxes for the branching process using the if else statement :
score = 200 if score <= 20 : result = "Unfortunately, your final grade is E, you need to retake the class in the next semester"; elif score <= 40 : result = "Unfortunately, your final grade is D, you have to proceed on the remedial exam and ask for additional assignment"; elif score <= 60 : result = "Dont be disappointed, although your final grade is C, ask for additional assignment to improve your grade"; elif score <= 80: result = "Your final grade is B, you can still be better than this"; elif score <= 100: result = "Congratulations, your final grade is A, keep the good work"; else: result = "Sorry, your grade cannot be evaluated..." print(result)
The execution of the above command will produce an output as follows :
[root@10 python]# python3 if-elif-else.py Sorry, your grade cannot be evaluated [root@10 python]#
The execution of the python script above obviously start from the top to the bottom of the line of codes. But at a certain line, there is a branching of the process where the execution of it depends on a certain condition or circumstance.
In the case of a success and meet the condition in the if statement, it will stop the process. Eventually, it will ignore all the other elif statement. But it will continue on the process if it does not meet the condition in the if and elif statement. So, it will stop until it meet the condition in one of the if or elif statement. Finally, if the process fail to meet every if and elif statement, it will absolutely execute the last else statement block.
Since the above variable’s value is 200, it will never meet the condition in all of the if and elif statement. In the end, it will then print the string of ‘Sorry, your grade cannot be evaluated…’. As there are no single block if and elif statement that can match the score value condition.