How to use if as a control flow in Python

Posted on

Introduction

This article is focusing on how to use the if reserved keyword in Python programming language. It is actually a reserved keyword which is very useful as a control flow in Python programming language. Actually, this article is getting into more details about the ‘if’ reserved keyword as a control flow. So, in almost every programming language including Python programming language, it is quite normal to have a control flow feature. The term control flow feature is a feature which is allowing to make branch of execution using a control mechanism. One of that control mechanism to branch the execution of the program is the ‘if’ reserved keyword in Python programming language.

How to use if as a control flow in python

In this part, the focus will be on how to be able to use the control flow using ‘if’ reserved keyword. In order to do that, start with learning from the syntax. The following is the simple syntax which exist in order to perform control flow in Python programming language :

if conditional_statement : 
   statement;

Before going on to the Python programming language directly in the form of the source code, just simulate it in a scenario in the form of a pseudo code. For an example, the conditional statement in this context is to compare and to check whether 10 is greater than 9. Then, the pseudo code for comparing those values exist as follows :

if check_if_10_is_greater_than_9 : 
   print_to_the_output_device_that_10_is_greater_than_9

Using the above pseudo code, the following is a simple example in order to simulate or perform control flow :

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");
...
10 is greater than 9
>>>

As in the above Python programming language source code, it is obvious that the flow of the process will run inside the if block. The reason is because the requirement is met. The conditional statement which is expressed by “10 > 9” is true. Since it is true, the if control flow will force the process to get inside the if block in order to process all of the statements exist in it. In this example, it is just to print and give an info that 10 is greater than 9.

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

Leave a Reply