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:
  1. if-else
  2. Nested if-else
  3. for
  4. while
  5. break
  6. Continue
  7. 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
  1. if(<condition>):    
  2.     Statement 1    
  3.     ...    
  4. else:    
  5.     Statement 2    
  6.     ...   
Understand this statement with a flow chart.
 
Python Flow Control Statements 
 
Example
 
Check In[3] and In[4] and also In[5] and In[6].
 
Python Flow Control Statements 
 

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
  1. if  (<condition 1>):    
  2.     Statement 1    
  3.     ...    
  4. elif (<condition 2>):    
  5.     Statement 2    
  6.     ...    
  7. else    
  8.     Statement 3    
  9.     ...   
Flow chart of Nested-if else
 
Python Flow Control Statements 
 
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 
 

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
  1. for value in sequence:    
  2.     ...body statement of for   
Flow chart of for statement
 
Python Flow Control Statements 
 
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 
 
 

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
  1. while(<condition>):    
  2.     statement 1.
Flow chart of while loop
 
Python Flow Control Statements 
 
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 
 

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
  1. for value in sequence:    
  2.     ...body statement of for    
  3.     if(<condition>):    
  4.        break    
  5.     ...body statement of for loop    
  6.       
  7. ...body statement outside of for loop   
III. with a while loop
  1. while(<condition>):    
  2.     statement 1...    
  3.    if(<condition>):    
  4.         break    
  5.    ...Statement of while loop    
  6.         
  7. ....Statements outside while loop   
Break statement Flow Chart
 
Python Flow Control Statements
 
Example
 
Check In[13] and In[14].
 
Python Flow Control Statements 
 

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
  1. for value in sequence:    
  2.     ...body statement of for    
  3.     if(<condition>):    
  4.         continue    
  5.     ...body statement of for loop    
  6.     
  7. ...body statement outside of for loop 
III. with the while loop
  1. while(<condition>):    
  2.     statement 1...    
  3.     if(<condition>):    
  4.         continue    
  5.     ...Statement of while loop    
  6.        
  7. ...Statements outside while loop   
    Continue statement Flow Chart
     
    Python Flow Control Statements 
     
    Example
     
    Check In[17] and In[19].
     
    Python Flow Control Statements 
     
    Python Flow Control Statements 
     
    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 
     
    It executes nothing.
     

    Conclusion

     
    In the next chapter, we will learn about Python For loops.
    Author
    Dipa Mehta
    145 12.6k 496.1k
    Next » Python While Loop