break
- We can use break statement inside loops to break loop execution based on some condition.
Example
Python
for i in range(10):
if i==7:
print("processing is enough..plz break")
break
print(i)
Output
PowerShell
0
1
2
3
4
5
6
processing is enough..plz break
Example
Python
cart=[10,20,600,60,70]
for item in cart:
if item>500:
print("To place this order insurence must be required")
break
print(item)
Output
PowerShell
10
20
To place this order insurence must be required
continue
- We can use continue statement to skip current iteration and continue next iteration.
Question
To print odd numbers in the range 0 to 9.
Python
for i in range(10):
if i%2==0:
continue
print(i)
Output
PowerShell
1
3
5
7
9
Example
Python
cart=[10,20,500,700,50,60]
for item in cart:
if item>=500:
print("We cannot process this item :",item)
continue
print(item)
Output
PowerShell
10
20
We cannot process this item : 500
We cannot process this item : 700
50
60
Example
Python
numbers=[10,20,0,5,0,30]
for n in numbers:
if n==0:
print("Hey how we can divide with zero..just skipping")
continue
print("100/{} = {}".format(n,100/n))
Output
PowerShell
100/10 = 10.0
100/20 = 5.0
Hey how we can divide with zero..just skipping
100/5 = 20.0
Hey how we can divide with zero..just skipping
100/30 = 3.3333333333333335
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!