Control Statements
Control Statements
Python program to check given number is prime or not using while with
else
Example:ex11.py
Break and continue statement
Break statement can be used inside for or while loop to skip the remaining
iterations and come out of loop.
#program to illustrate break statement
x=10
while x>=1:
print(“x=“,x)
x-=1
if x==5:
break
print(“out of loop”)
Continue statement
Continue statement will skip particular iteration of loop and continues
with next iteration
#python program to print numbers from 1 to 10 and skip 6
for i in range(1,10):
if i==6:
continue
print("i=",i)
print("out of loop")
pass statement
Sometime we do not have any statements to write in a block. In python block
can not be empty. If we do not have any statements to write then we must
use pass keyword which indicates null statement
Example: Check number that is not divisible by three
x=5
if x%3!=0:
print(x)
Or
x=5
if x %3==0:
pass
else:
print(x)
assert statement
assert statement is useful to check if a particular condition is fulfilled or not.
syntax
assert expression, message
In the above syntax the ‘message’ is not compulsory.
"""
python program to assert that the user enters a
number greater than zero
"""
x=int(input('Enter a number greater than 0:'))
assert x>0, "worng input entered"
print("U entered a number",x)
output
Enter a number greater than 0:6
U entered a number 6