0% found this document useful (0 votes)
16 views3 pages

Break Continue Pass

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Break Continue Pass

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Premature Termination of Loops

In Python, break and continue statements can alter the flow of a normal loop.
Loops iterate over a block of code until test expression is false, but sometimes we wish to terminate
the current iteration or even the whole loop without checking test expression. The break and
continue statements are used in these cases.
Python break statement

The break statement terminates the loop containing it. Control of the program flows to the
statement immediately after the body of the loop.

If break statement is inside a nested loop (loop inside another loop), break will terminate the
innermost loop.

for val in "string": s i=1 1


if val == "i": t while i < 6: 2
break r print(i) 3
print(val) The end if i == 3: The end
print("The end") break
i += 1
print("The end")
number=0 Enter numbers to Enter numbers to
sum=0 sum, zero or negative sum, zero or negative
print("Enter numbers to sum, zero or negative number to end the list number to end the list
number to end the list") 2 5
while True: 3 3
number=eval(input()) 4 4
if number <=0: 1 2
break 4 -1
sum+=number 0 The sum of the
print (" The sum of the numbers is ", sum) The sum of the numbers is 14
numbers is 14
for num in [11, 9, 88, 10, 90, 3, 19]: 11
print(num) 9
if(num==88): 88
print("The number 88 is found") The number 88 is found
print("Terminating the loop") Terminating the loop
break
Python continue statement

The continue statement is used to skip the rest of the code inside a loop for the current iteration
only. Loop does not terminate but continues on with the next iteration.

for val in "python":


if val == "h": p
continue y
print(val) t
print("The end") o
n
The end

for num in [20, 11, 9, 66, 4, 89, 44]: 11


# Skipping the iteration when number is even 9
if num%2 == 0: 89
continue
# This statement will be skipped for all even numbers
print(num)

i=0 1
while i < 6: 2
i += 1 4
if i == 3: 5
continue 6
print(i)
Python pass statement

In Python programming, pass is a null statement. The difference between


a comment and pass statement in Python is that, while the interpreter ignores a comment
entirely, pass is not ignored. The pass statement acts as a placeholder and usually used when
there is no need of code but a statement is still required to make a code syntactically correct.

However, nothing happens when pass is executed. It results into no operation (NOP) or it says the
interpreter to do nothing.

Syntax of pass
pass

We generally use it as a placeholder.

Suppose we have a loop or a function or an if-elif statement that is not implemented yet, but we
want to implement it in the future. They cannot have an empty body. The interpreter would
complain. So, we use the pass statement to construct a body that does nothing.

print("Select what you want to find")


print("1= Area of circle")
print("2= Area of rectangle")
print("3= Circumference of circle")
print("Enter the choice as 1-3")
n1=int(input())
if n1==1:
print("Enter the radius")
r=float(input())
ans=3.14*r*r
elif n1==2:
pass
# Not finished yet
elif n1==3:
pass
# Not finished yet
else:
ans="Invalid Number,Enter numbers from 1-3"
print(ans)
print("Program Over")

You might also like