Table of Contents
Unconditional control statement in Python
- The Unconditional control statement is way to control the statement without using direct conditions.
- There are two types :
- break
- continue
Break
- The break statement is used to terminate loops or exit the loops.
- It is necessary to exit immediately from a loop as soon as the condition is satified.
- The statements after break statement are skipped.
Syntax:
while condition:
break
Example of break
Break use in while loop
Break Use in infinite Loop
Mini Calc
Break use in while loop
i=1
while i<=10:
print(i)
if i==2:
break
i=i+1
Break Use in infinite Loop
i=1
while True:
print(i)
if i==2:
break
i=i+1
Mini Calc
while True:
print("Press 1 for add")
print("Press 2 for sub")
print("press 3 for mult")
print("press 4 for exit")
x=int(input())
if x==1:
print("Enter the first number")
a=int(input())
print("Enter the second number")
b=int(input())
print("Your sum=",a+b)
elif x==2:
print("Enter the first number")
a=int(input())
print("Enter the second number")
b=int(input())
print("Your sub=",a-b)
elif x==3:
print("Enter the first number")
a=int(input())
print("Enter the second number")
b=int(input())
print("Your mul=",a*b)
elif x==4:
break
else:
print("you choice worng input plz.. try again")
print("thank you visit again.....")
Continue
- Sometime , it is required to skip a part of a body of loop under specific conditions.
- The working structure of continue is similar as that of that the break statement but difference is that it cannot terminate the loop.
- It causes the loop to be continued with next iteration after skipping statements in between.
- Continue statement simply skips statements and continues next iteration.
Syntex:
while condition:
if condition:
continue
Example of Continue in python
continue Use in while Loop
continue Use in infinite Loop
continue Use in while Loop
i=1
while i<=10:
print(i)
if i==2:
break
i=i+1
continue Use in infinite Loop
i=1
while True:
print(i)
if i==2:
continue
i=i+1