How to Use if else as a control flow in Python

Posted on

Introduction

This is an article which is the expansion from a general article exist in the previous one. In the previous article with the title of ‘Control Flow in Python’ in this link, It is also variety of another simple control flow function exist in Python programming language using if reserved keyword. For more information about the if reserved keyword as the control flow in Python programming language, just visit this link. In that link, there is an article with the title of ‘How to use if as a control flow in Python’. On the other hand, this article will show how to use if else reserved keyword. If else reserved keyword is also one of the control flow function exist in Python programming language.

How to use if else as a control flow in python

So, this part is the part where the functionality for performing control flow in Python programming language will be discussed in detail. First of all, starting from the syntax. In case of if else reserved keyword, the following is the syntax for performing control flow :

if conditional_statement : 
   statement;
else :
   statement; 

Using the above syntax, just try to transform it first into a pseudo code. Actually, it is very useful to describe the scenario or the situation for the implementation. Below is the pseudo code for the if else reserved keyword as a control flow :

if check_if_10_is_greater_than_9 : 
   print_to_the_output_device_that_10_is_greater_than_9
else:
   print_to_the_output_device_that_10_is_not_greater_than_9

Actually, the above pseudo code is only just example. Using a simple example in the conditional_statement to compare the value of 9 and 10. In order to simulate or to perform control flow, below is the execution of it in the form of Python programing language’s source code :

C:\>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> if 10 > 9 :
... print("10 is greater than 9");
... else:
... print("10 is not greater than 9");
...
10 is greater than 9
>>>

Actually, the above source code is the modification from the example exist before. That example exist in the article which is focusing to discuss about how to use the if reserved keyword as control flow. As a reference, it is exist in this link. That article with the title of ‘How to use if as a control flow in Python’ is showing only the ‘if’ block part for execution. On the other, this article has an additional ‘else’ block part. So, there is two alternate process exist. Depends on the conditional statement, it will execute only one block of the statements. As in the above example, since 10 > 9, it will execute the statements exist inside the if block. For more example, below is the alternate version where the execution process is in the else block instead :

>>> if 9 > 10 :
... print("9 is greater than 10");
... else:
... print("9 is not greater than 10");
...
9 is not greater than 10
>>>

In case of the the above Python programming language source code, it is obvious that the flow of the process will run inside the else block. The reason for that is simple. It is because the requirement for fulfilling the conditional statement in if part is not met. So, the conditional statement of “9 > 10” is not true. Because it is not true, it will pass the statement exist inside the if control block. Furthermore, the process will just run and direct it into the else block. And in that else block, it it is just to print and give an info that 9 is not greater than 10.


One thought on “How to Use if else as a control flow in Python

Leave a Reply