Introduction
This article is still a part of the article describing about how to perform branching to control logic flow in programming language. It is the continuation of another article with the title of ‘How to perform branching to control logic flow in python programming language using if statement’ in this link. In general, almost every programming language has a feature or concept for controlling the logic flow of the execution of process. That feature or concept is the branching process. In the branching process, the control of the logic flow of the execution of the process can be defined. The feature or concept of branching process is available where several alternative flows is possible upon the execution of that process. Just set the logic of the execution in order to control the flow of the process.
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 nested if statement. The term nested means that the if statement is nested in another if statement. In other words, there will be another if statement inside an if statement. How many if statement available ?. Well, it depends on the definition of the condition.
Branching Example using Nested If Statement
Fulfilling the condition is also a necessary in the nested if statement. Actually, since there can be more than one if statement, it is possible that there will be several branching process execution. As long as the condition in the definition is met in the if statement. So, the following are the syntaxes for the branching process using the nested if :
score = 85 result = "Sorry, your grade cannot be evaluated…" if score <= 100: result = "Congratulations, your final grade is A, keep the good work…"; if score <= 80: result = "Your final grade is B, you can still be better than this…"; if score <= 60 : result = "Don’t be disappointed, although your final grade is C, ask for additional assignment to improve your grade…"; if score <= 40 : result = "Unfortunately, your final grade is D…, you have to proceed on the remedial exam and ask for additional assignment"; if score <= 20 : result = "Unfortunately, your final grade is E…, you need to retake the class in the next semester"; print(result)
The output of the above command exist as follows :
[root@10 python]# python3 nested-if.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. There are only one big block of branch process. Furthermore, inside that one big block, there are more if statements available inside of it. If that variable pass the condition defined in the branch process statement, it will then route the flow of the process to execute the inner line of code inside the branch process block. Inside the block, a repeatable process for checking the condition in the if statement will proceed. The repetition will going further until there are no if statement available. It will stop in the last inner if statement.
Since the above variable’s value is 85, it only meet the condition or the requirement defined in the branch process. It will then start to check for the inner if statement condition. Actually, since it fail to meet the condition since it is greater than 80, the process will finally stop. So, 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.
2 thoughts on “How to perform branching to control logic flow in python programming language using nested if statement”