- break statement
- continue statement
- pass statement
1.) The break statement:-
Program 1:Write a python program to print the given string using break statement.
string="MNT-LAB"
count=1
i=0
for i in string:
if(i=='A'):
break
print(i)
count=count+1
print("character 'A' is matched at the count=",count)
Output:-M
N
T
-
L
character 'A' is matched at the count= 6
Program 2:Write a python program to print the numbers using break statement.
i=1
while(1):
print(i)
i=i+1
if(i==12):
break
print("Control come out of the loop")
Output:-
1
2
3
4
5
6
7
8
9
10
11
Control come out of the loop
>>>
Program 3:Write a python program to print multiplication values using break statement.
a=5
i=1
for i in range(1,20,5):
print("%dx%d=%d \n"%(a,i,a*i))
choice=int(input("Do you want to continue press:1\n"))
a=a+1
if(a==7):
break
print("Now you are out of the for loop")
Output:-
5x1=5
Do you want to continue press:1
1
6x6=36
Do you want to continue press:1
1
Now you are out of the for loop
>>>
Program 4:Write a python program to print list value using break statement.
i=0
list=['a','e','i','o','u']
for i in list:
if(i=='u'):
break
print(i)
print("out of the for loop")
Output:-
a
e
i
o
out of the for loop
>>>
2.) The continue statement:-
- The continue statement is used as a keyword in python.
- The continue statement is used to get back the program control to the starting of the loop.
- The continue statement is used to skips the remaining of the code inside the loop and start with next iteration of the loop.
- One things keep in your mind,when we use continue statement, loop does not terminate but program control goes to next iteration of the loop.but in case of break statement loop gets terminate immediately.
Program 1:Write a python program to print give string values using continue statement.
#str="MNT-LAB"
str=input("Enter any string:\n")
for i in str:
if(i=='L'):
continue
print(i)
print("Now I am out of the loop")
Output:-
Enter any string:
MNT-LAB
M
N
T
-
A
B
Now I am out of the loop
>>>
Program 2:Write a python program to print upto the given number values using continue statement.
num=int(input("Enter the Number:\n"))
for i in range(0,num):
if(i==6):
continue
print(i)
print("Now I am out of the loop")
Output:-
Enter the Number:
10
0
1
2
3
4
5
7
8
9
Now I am out of the loop
>>>
3.) The pass statement:-
- The pass statement is also a python keyword.
- The pass statement is a empty (null) operation,when we use this statement ,nothing happen but you avoid getting an error.
- The is more helpful to run the empty code of the program and avoid the error to run the program.because we already know, empty code is not allowed in loops,functions definitions,class definitions or in if-else statements.
- The pass statement can be used while overriding a parent class method in the child class.
- The pass statement is used if the code will be written outside of the program but not yet written in the program file.
Program 1:Write a python program to print the empty code using pass statement.
#This is an empty loop
for i in range(1,10):
pass
#This is an empty method
def calculate():
pass
#This is a class
class student:
pass
#This is a if-else statement
a=8
b=10
c=20
if(a<b):
pass
elif(b<c):
pass
else:
pass
Output:-
========== RESTART: C:/Users/RAMASHANKER VERMA/Desktop/empty codes.py ==========
>>>
Descriptions:-
- This above python codes give empty(null) output but this python program is running .This is not get any error due pass statement.
- If you remove the pass statement, this program gives error.
- The pass statement is more helpful to run the program by avoiding an error in python code.
Program 2:Write a python program to print upto given numbers using pass statement.
num=int(input("Enter the Number:\n"))
i=0
while(i<=num):
if(i==5):
pass
print("The pass statement pass the value at i=",i)
print(i)
i=i+1
print("I am out of the loop")
Output:-
Enter the Number:
10
0
1
2
3
4
The pass statement pass the value at i= 5
5
6
7
8
9
10
I am out of the loop
>>>
Program 3:Write a python program to print the list valuse using pass statement.
list=[10,20,30,40,50,60,70,'a','Ram']
for i in list:
if(i==60):
pass
print("Program control pass to next iteration at i=",i)
print(i)
Output:-
10
20
30
40
50
Program control pass to next iteration at i= 60
60
70
a
Ram
>>>
Note:-
Watch complete Lecture video:
0 comments:
Post a Comment