Control Statements in Python

We know that loops are used to execute a set of statements repeatedly sometimes we need to control execution of the program based on the values and the conditions, so to accomplish this task Control Statements are using in python ,python support three types of Control Statements Continue Statement,Break Statement and the Pass Statement. Lets see in details one by one

Continue Control Statement in Python

Sometimes we need to exclude particular iteration inside the loop in that case “Continue” Control Statements is used to accomplish control to the beginning of the loop.if the condition is true it will return to the beginning of the loop.

Example


# Continue Control Statement in Python

#for loop to print a number between 1 to 4 without 2

l = [1, 2, 3, 4] 
for i in l: 
    if i==2:
       continue
    print(i) 



Out put
1
3
4

Break Control Statement in Python

Sometimes we need to stop the iteration based on particular logic or condition Break Statement is used to accomplish this task .Break statement break stop the execution of the loops

Example

# Break Control Statement in Python


l = range(1, 100)
for i in l: 
    if i==10:
       break   #stop the execution here if i equal to 10
    print(i) 

Output

1
2
3
4
5
6
7
8
9

Pass Control Statement in Python

If we want to execute nothing pass statement can be used. Sometimes we need to implement functionality code after beta delivery of the project we use “pass” to keep a loop without anything

l = [1, 2, 3, 4] 
for i in l :  
    pass

Leave a Reply

Your email address will not be published. Required fields are marked *