We can use break statement inside loops to break loop execution based on some condition.
for i in range(10): if i==7: print("Processing is enough..please break the loop") break print(i)
0 1 2 3 4 5 6 Processing is enough..please break the loop
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)
10 20 To place this order insurence must be required
We can use continue statement to skip current iteration and continue next iteration
E.g: To print odd numbers in the range 0 to 9
for i in range(10): if i%2==0: continue print(i)
1 3 5 7 9
Loops with else Block
- Inside loop execution, if break statement not executed, then only else part will be executed.
- else means loop without break.
cart=[10,20,30,40,50] for item in cart: if item>=500: print("We cannot process this order") break print(item) else: print("Congrats...all items processed successfully")
10 20 30 40 50 Congrats...all items processed successfully
3. pass statement
- pass is a keyword in Python.
- In our programming syntactically if block is required which won’t do anything then we can define that empty block with pass keyword.
- This statement does nothing. It is used to define an empty block of code or a class. When written in a loop statement, it’s usually the last statement.
numbers = [10,11,12,13,14] for num in numbers : if num%2 == 0: print(num) else pass