0% found this document useful (0 votes)
3 views

9 Python Programming

Uploaded by

Rudraaksh Sethi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

9 Python Programming

Uploaded by

Rudraaksh Sethi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

LOOPS, CONTINUE,

BREAK STATEMENT
Session 9
Break, Pass, Continue statement
◦ Using loops in Python automates and repeats the tasks in an efficient manner.
◦ Sometimes, there may arise a condition where you want to exit the loop
completely, skip an iteration or ignore that condition. These can be done by loop
control statements.
◦ Loop control statements change execution from its normal sequence.

Python supports the following control statements.


◦ Continue statement
◦ Break statement
◦ Pass statement
Break Statement
• Break statement in Python is used to
bring the control out of the loop
when some external condition is
triggered.
• Break statement is put inside the
loop body (generally after if
condition).
Example
s=‘abcdefghijknmnms’
Output
for letter in s: ------------
a
print(letter) b
# break the loop as soon it sees 'e’ or 's' c
d
if letter == 'e' or letter == 's': e
Out of for loop
break

print("Out of for loop")


Example –Practical use of break statement
◦ #Program for prime number

N= eval(input(“enter a number”))
flag=0
For i in range(2, N+1) :
if N % i== 0:
flag=1
break #if we find a factor we can break out as it gets confirmed no is not prime
if flag==0:
print(“ Number is prime”)
else:
print(“ Nota prime number”)
Continue statement
Continue is also a loop control statement just like the break statement.
continue statement is opposite to that of break statement, instead of
terminating the loop, it forces to execute the next iteration of the loop.
The continue statement forces the loop to continue or execute the next
iteration.
When the continue statement is executed in the loop, the code inside the loop
following the continue statement will be skipped and the next iteration of the
loop will begin.
Example
for i in range(1, 11):
# If i is equals to 6, continue to next iteration Output:
if i == 6:
1 2 3 4 5 7 8 9 10
continue
else:
print(i, end = " ") # otherwise print the value of i
Pass Statement
The pass statement is a null statement.
But the difference between pass and comment is that comment is ignored by
the interpreter whereas pass is not ignored.

The pass statement is generally used as a placeholder i.e. when the user does
not know what code to write. User simply places pass at that line.
Sometimes, pass is used when the user doesn’t want any code to execute.
So user simply places pass there as empty code is not allowed in loops, function
definitions, or in if statements. Using pass statement user avoids this error.
Example
a = 10
b = 20

if(a<b):
pass
else:
print("b<a")
Program to find factorial using while
loop
N= eval(input(“ Enter a number”))
Factorial = 1
i=1
While i<=N :
factorial = factorial * i
i= i+1
Print(“factorial –”, factorial)
GCD or HCF of 2 numbers
x=eval(input(“ Enter first integer”))
y=eval(input(“ Enter second integer”))
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
print(“ hcf is “,hcf)
row = int(input('Enter how many rows‘))
1
# Generating pattern
212 for i in range(1,row+1):
32123
# for space printing
4321234
for j in range(1, row+1-i):
print(' ', end=‘ ')

# for decreasing pattern


for k in range(i,0,-1):
print(k, end=‘’)

# for increasing pattern


for p in range(2,i+1):
print(p , end=‘ ')

# Moving to next line


print()

You might also like