How to perform branching to control logic flow in python programming language using if elif statement

Posted on

Introduction

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 the continuation of another article with the title of ‘How to perform branching to control logic flow in python programming language using nested if statement’ in this link. So, in this branching process, the control of the logic flow of the execution of the process’s definition is using the if else statement. By doing it, the branching process is available where several alternative flows is possible upon the execution of that process. It will happen by setting the logic of the execution in order to control the flow of the process in the if else statement.

In python programming language, branching process feature or concept is available using several syntaxes. In this article, the syntax for branching the process is using the if elif statement. The term if else 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 statement available ?. Well, 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. It will not look for the other if statement in the elif statement block for further execution.

Branching Example using If Elif Statement

Fulfilling the condition is also a necessary in the if elif statement. There are more than one if statement, so there are several branching process execution. But in the reality, there will be only one process execution at maximum. So, if it fail to meet all of the conditions, there will be no execution in even one single if statement block. So, the following are the syntaxes for the branching process using the if elif statement :

score = 85                                                                                                                                                                  result = "Sorry, your grade cannot be evaluated"

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";

print(result)

The output of the above command exist as follows :

[root@10 python]# python3 if-elif.py
Congratulations, your final grade is A, keep the good work
[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. Furthermore, it will also stop if it fail in every condition in the if or elif statement. In reality, it will try to check the condition in every single if available. In the end, if it fails in every single if or elif statement, it will also stop. It stop until there is no more if or elif statement left.

Since the above variable’s value is 85, it only meet the condition or the requirement defined in the last elif statement. It will check the condition in every elif statement since in the first if it fails to meet the condition. Finally, it will then print the string of ‘Congratulations, your final grade is A, keep the good work…’. Just change the score value for more variable output result.

Leave a Reply