Python Flow Control Statements
Introduction
In this chapter, we will learn about various flow control statements.
What are the Flow Control Statements?
A Flow Control Statement defines the flow of the execution of the Program.
There are 7 different types of flow control statements available in Python:
- if-else
- Nested if-else
- for
- while
- break
- Continue
- Pass
Let’s learn about these all statements in detail.
if-else
If-else is used for decision making; when code statements satisfy the condition, then it will return non-zero values as true, or else zero or NONE value as false, by the Python interpreter.
Syntax
- if(<condition>):
- Statement 1
- ...
- else:
- Statement 2
- ...
Understand this statement with a flow chart.
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf1.png)
Example
Check In[3] and In[4] and also In[5] and In[6].
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf4.png)
Nested if-else
With if...elif...else, elif is a shortened form of else if. It works the same as 'if' statements, where the if block condition is false then it checks to elif blocks. If all blocks are false, then it executes an else statement. There are multiple elif blocks possible for a Nested if...else.
Syntax
- if (<condition 1>):
- Statement 1
- ...
- elif (<condition 2>):
- Statement 2
- ...
- else
- Statement 3
- ...
Flow chart of Nested-if else
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/PF2.png)
Remember there is no condition statement associated with else part of these flow control statements. It will execute ig statements only in the case that all conditions are false.
Example
Check In[3] and In[8] and also In[9] and In[10].
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf5.png)
for Statement
The for loop statement has variable iteration in a sequence(list, tuple or string) and executes statements until the loop does not reach the false condition.
Syntax
- for value in sequence:
- ...body statement of for
Flow chart of for statement
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf3.png)
Example
Check In[14] and In[16]. The continue statement is used to stop for loop, in case there is an else block missed.
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf6.png)
while loop
A while loop is used in python to iterate over the block of expression which matches to true. Non-zero values are True and zero and negative values are False, as interpreted in Python.
Syntax
- while(<condition>):
- statement 1..
Flow chart of while loop
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf7.png)
Example
Check In[4] and In[7]. In[7]. If the user wants to add a number of his choice, then use: n = int(input("Enter number: ")) instead of n=20. Also, check-in In[3] for a while..else loop.
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf8.png)
Break statement
The Python Break statement is used to break a loop in a certain condition. It terminates the loop. Also, we can use it inside the loop then it will first terminate the innermost loop.
Syntax
I. break
II. with for loop
- for value in sequence:
- ...body statement of for
- if(<condition>):
- break
- ...body statement of for loop
- ...body statement outside of for loop
III. with a while loop
- while(<condition>):
- statement 1...
- if(<condition>):
- break
- ...Statement of while loop
- ....Statements outside while loop
Break statement Flow Chart
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf10.png)
Example
Check In[13] and In[14].
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf12.png)
Continue Statement
A continue statement won’t continue the loop, it executes statements until the condition matches True.
Syntax
I. continue
II. with for loop
- for value in sequence:
- ...body statement of for
- if(<condition>):
- continue
- ...body statement of for loop
- ...body statement outside of for loop
III. with the while loop
- while(<condition>):
- statement 1...
- if(<condition>):
- continue
- ...Statement of while loop
- ...Statements outside while loop
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf13.png)
Example
Check In[17] and In[19].
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf14.png)
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf15.png)
One more additional Flow statement is PASS.
PASS
In Python, pass, and comment both are quite similar. The pass contains a Null value. The Python interpreter ignores the comment statement, but it’s not possible to ignore a pass statement. There is nothing that is going to execute when a pass is used. It is used as a Place Holder in a loop or a function.
Example
Check below In[21].
![Python Flow Control Statements](https://www.csharp.com/UploadFile/Tutorial/admin/python-flow-control-statements02072020042044/Images/pf16.png)
It executes nothing.
Conclusion
In the next chapter, we will learn about Python For loops.
Author
Dipa Mehta
147
12.6k
504.1k