Break, Continue and Pass statement in python
Break Statement:
Break in python used to break or terminate the loop control and transfer the control to the statement immediately of following the loop.
Syntax:
loop statement
break;
Example:
Program to display multiplication table using user choice.
while 1:
n=int(input("Enter no:"))
i=1
while(i<=10):
print("%d X %d = %d\n"%(n,i,n*i))
i=i+1
choice=int(input("Do you want to continue printing the table, press 0 for no 1 for yes ?"))
if(choice==0):
break;
Output:
Enter no:2
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
Do you want to continue printing the table, press 0 for no 1 for yes ?1
Enter no:5
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
Do you want to continue printing the table, press 0 for no 1 for yes ?0
Syntax:
loop statement
break;
Example:
Program to display multiplication table using user choice.
while 1:
n=int(input("Enter no:"))
i=1
while(i<=10):
print("%d X %d = %d\n"%(n,i,n*i))
i=i+1
choice=int(input("Do you want to continue printing the table, press 0 for no 1 for yes ?"))
if(choice==0):
break;
Output:
Enter no:2
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
Do you want to continue printing the table, press 0 for no 1 for yes ?1
Enter no:5
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
Do you want to continue printing the table, press 0 for no 1 for yes ?0
Continue Statement:
Continue statement in Python allow to bring the program control to the beginning of the loop. It skip the remaining code of the inside loop and start from the next iteration.
Syntax:
loop statement
continue
code to be skipped
Example:
Program to display the numbers:
for x in range(10):
if(x==3):
continue
print(x)
Output:
0
1
2
4
5
6
7
8
9
Syntax:
loop statement
continue
code to be skipped
Example:
Program to display the numbers:
for x in range(10):
if(x==3):
continue
print(x)
Output:
0
1
2
4
5
6
7
8
9
Pass Statement:
Pass statement is used when you do not want to execute any code or command. It is a null operation, nothing will happened when it is execute. It is basically use when we bypass any code or statement.
Syntax:
pass
Example:
Program to display the numbers:
for l in 'Python':
if l== 'o':
pass
print('This is pass block')
print('Current Letter :',l)
print("Bye!")
Syntax:
pass
Example:
Program to display the numbers:
for l in 'Python':
if l== 'o':
pass
print('This is pass block')
print('Current Letter :',l)
print("Bye!")
Output:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
This is pass block
Current Letter : o
Current Letter : n
Bye!
Comments
Post a Comment
If you have any doubt, Please let me know..