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 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.
Example
Check In[3] and In[4] and also In[5] and In[6].
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
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].
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
Example
Check In[14] and In[16]. The continue statement is used to stop for loop, in case there is an else block missed.
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
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.
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
Example
Check In[13] and In[14].
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
Continue statement Flow Chart
Example
Check In[17] and In[19].
One more additional Flow statement is 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].
It executes nothing.
In the next chapter, we will learn about Python For loops.