Table of Contents
Control statements in python
Python consists of control statements to have control on functioning of loops.
This control statements are also called as terminating loops or skipping specific conditions.
The objects created during the normal execution are also destroyed.
Control statements includes break, continue and pass.

These statements are used to alter the normal flow of execution.
Break Statement (terminating loops)
The break statement is used to terminate the loop and to transfer the control of execution to the statement immediately following the loop.
If we are using nested loop than break statement will only exit the loop in which it is present. This means that break statement only exits single loop.
Break statement can be used to inject interruption externally by triggering some condition.
Both for loop and while loop can have break statement in it.
Break statement is executed when certain condition is satisfied as per the requirement of script.

Syntax
break
1. Example of break control statement in for loop
""" Author : ITVoyagers (itvoyagers.in) Date :25th August 2020 Description : Program to show use of break control statement in for loop """ for i in 'itvoyagers': if i=='y': break else: print("Current value of iterator is ", i ) print("I am printed as break statement terminated the loop")
Output

2. Example of break control statement in while loop
""" Author : ITVoyagers (itvoyagers.in) Date :25th August 2020 Description : Program to show use of break control statement in while loop """ j=5 while j>0: print("current value of iterator is ",j) j-=1 if j==2: break print("I am printed as break statement terminated the loop")
Output
Continue Statement (skipping specific conditions)
The continue statement is used to skip the execution of the loop when a specific condition is encountered and transfers the control of execution to the start of the current loop and without executing other statement it again checks the condition before reiterating the loop.
If we are using nested loop than continue statement will only skip the specific condition present in single loop. .
Continue statement can be used to inject interruption externally by triggering some condition.
Both for and while loop can have continue statement in it.
Continue statement is executed when certain condition is satisfied as per the requirement of script.

Syntax
continue
1. Example of continue control statement in for loop
""" Author : ITVoyagers (itvoyagers.in) Date :25th August 2020 Description : Program to show use of continue control statement in for loop """ for i in 'itvoyagers': if i=='y': continue else: print("Current value of iterator is ", i ) print("I am printed after complete execution of script")
Output

2. Example of continue control statement in while loop
""" Author : ITVoyagers (itvoyagers.in) Date :25th August 2020 Description : Program to show use of continue control statement in while loop """ j=10 while j>0: j-=1 if j==8 or j==3: continue print("current value of iterator is ",j) print("I am printed after complete execution of script")
Output
Pass Statement
The pass statement is used when a statement is required syntactically but you do not want any command or script to be executed.
The pass statement is null operation having no effect on program execution.
Suppose if you want to create a function or method for future use and if you are still not defining its body then it is syntactically incorrect and to avoid such situation we can use pass statement.
Basically it will reserve that function name for future use and also function body can be defined later and script can be executed without any error.
Pass statement is similar to comment as both does not effect the flow of execution but only difference is that comments are generally ignored by interpreter but pass statement cannot be ignored.
Syntax
pass
1. Example of pass control statement in for loop
""" Author : ITVoyagers (itvoyagers.in) Date :25th August 2020 Description : Program to show use of pass control statement in for loop """ for i in 'itvoyagers': if i=='y': pass print("-----------------Pass statement is used----------------------") print("Current value of iterator is ", i ) print("I am printed after complete execution of script")
Output

2. Example of pass control statement in while loop
""" Author : ITVoyagers (itvoyagers.in) Date :25th August 2020 Description : Program to show use of pass control statement in while loop """ j=10 while j>0: j-=1 if j==8 or j==3: pass print("-----------------Pass statement is used----------------------") print("current value of iterator is ",j) print("I am printed after complete execution of script")
Output
For other python basics related posts:
For other advanced python related posts: