Control Flow in Python

Posted on

Introduction

As a programming language, there is the term of control flow as an important aspect in Python programming language. In this article, the main focus will be discuss about how to perform a control flow in Python programming language. In Python programming language, there are several functionality which exist as a control flow feature. Those functionality for controlling flow will have just using a specific reserved keywords. Those reserved keywords for using or performing control flow feature is the ‘if’, ‘if-else’ and also ‘if-elif-else’. Actually, it is very simple to use or to perform a control flow. Details and descriptions about all of them exist in the following part.

Using if reserved keywords as control flow

The first one is using ‘if’ reserved keyword in order to perform the control flow. Before going on further, the following is the discussion on the scenario of the control flow :

if conditional_statement : 
   action

The scenario above consist of two components. The first one is the conditional_statement. It is a statement which is describing condition for further valuation or assessment. In other words, ‘if’, it meet the conditional_statement, it only the necessary requirement to redirect the execution flow of the process to be able to execute the ‘action’ exist after the ‘if conditional_statement’. So, the ‘if’ reserved keyword in this case acts as a control flow. It will control the flow of the process based on the condition expressed in the conditional_statement. So, as in the above pseudo code statement, using it as a base for defining the control flow, the following is the scenario of it in the form of Python programming language’s source code :

available_fund = 50000
price = 40000
if available_fund is > price :
   print('The product is affordable to buy')

Below is the actual execution of the above code in the Python command line console :

(env) C:\python>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.
>>> available_fund = 500000
>>> price = 400000
>>> if available_fund > price :
... print('The product is affordable to be bought');
...
The product is affordable to be bought
>>>

For more information about how to use if reserved keywords as a control flow, just visit this link. That link will direct to an article with the title of ‘How to use if as a control flow in Python’.

Using if-else reserved keywords as control flow

Another simple way to use or to perform a control flow, it is by using the ‘if-else’ reserved keyword. In case of ‘if’ reserved keyword which in Python programming language is available to control the flow, there is only one possible alternative flow in it. But in the case of ‘if-else’ reserved keyword as a feature for performing control flow, it can have two alternative flow. The first one exist in the ‘if’ part and the second one exist in the ‘else’ part. It exist as in the following scenario below :

if conditional_statement : 
   action_1
else: 
   action_2

So, as the program running, it will execute only one of the two action exist in the above scenario. Whether it is the action exist in the if block or the action exist in the else block. In other words, if the condition meet the one exist in the conditional_statement, the process flow will be directed to the ‘action’ in the if block. If it is not, it will execute the ‘action in the else block.

>>> available_fund = 500000
>>> price = 400000
>>> if available_fund > price :
...     print('The product is affordable to be bought');
... else:
...     print('The product is not affordable to be bought');
...
The product is affordable to be bought
>>>

The above is the flow for executing the action_1 since the condition meet where the available_fund is actually higher than the price. On the other hand, if it does not meet the condition exist in the conditional statement at the if block, then it will execution action_2. It is another action exist inside the else block. Below is the example for the execution :

>> available_fund = 500000
>> price = 600000
>> if available_fund > price :
… print(‘The product is affordable to be bought’);
… else:
… print(‘The product is not affordable to be bought’);

The product is not affordable to be bought
>>

So, in the above example, the context is reversed. Actually, as it exist In the above example, the price is higher than the available_fund. Therefore, the conditional statement in the if block is not met. In this case, it will run into the alternate flow which is an action exist in the else block. For more explanation, just visit the article with the title ‘How to Use if else as a control flow in Python’ in this link.

Using if-elif-else reserved keywords as control flow

Another control flow which is actually the modification and the adaptation from both two of previous versions. Adding another reserved keywords which is ‘elif’ before else. In that case, it will add another alternative flow. So, the total flow will become three flows each of them is the action in the if block, elif block and also else block. The pseucode for describing flow of the scenario exist below :

if conditional_statement : 
   action_1
elif conditional_statement : 
   action_2
else:
   action_3

Referring to the above pseudo code scenario, the following is the implementation using script where the execution process is in a Python command console as follows :

>>> available_fund = 500000
>>> price = 500000
>>> if available_fund > price :
...     print('The product is affordable to be bought');
... elif available_fund == price :
...     print('The product is affordable to be bought but no available fund left');
... else:
...     print('The product is not affordable to be bought');
...
The product is affordable to be bought but no available fund left
>>>

So, in the above script execution, there is an additional flow where the available_fund is actually have the same value with the price. It will take a different flow resulting on a different action by printing a different information. That information, in this case is informing that the product is available to be bought but no available fund left. Another detail explanation exist in another article exist in this link. It is an article with the title of ‘How to Use if elif else as a control flow in Python’.

Leave a Reply