In this tutorial, we are going to learn two Keywords break & continue and learn how and where to use them.
Table of Contents:
- What are break and continue keywords do in python
- Where to use a break and continue
- Python break keyword
- Python continue keyword
What are break and continue keywords do in python?
break and continue are two special keywords in python that often used with loops for and while to modify the flow of loops.
Loops are used to execute a statement again and again until the expression becomes False or the sequence of elements becomes empty. But what if, we want to terminate the loop before the expression become False or we reach the end of the sequence, there we need some keywords which can alter the functionality of loops.
Break in python
The break is a keyword often used with while and for loop to terminate its repetitive functionality. The moment the for loop find the break keyword inside its body the for loop stop to work and throw us out of the loop.
break keyword is used to counter the loop, so if you are using nested loop (a loop inside a loop) then the break will counter that loop only which loop define it.
for value in sequence: # code for for block if condition: break #code for for loop #outside of for loop while expression: #code for while loop if if_expression: break #code for while loop #outside of while loop
Let’s understand the break keyword with an example.
print("break for for loop") for number in [1,2,3,4,5,6,7,8,9,10]: print(number) if number > 6: break print("\nbreak for while") var = [1,3,4,5,6,7,8,9,10] i=0 while True: if var[i] > 5: break print(var[i]) i=i+1
break for for loop 1 2 3 4 5 6 7 break for while 1 3 4 5
In the above example, we have used break for both for and while loops. You can see that, the moment the if statements of both the loops find an expression True for themselves, they execute the break keyword and the break keyword directly affect the loops and brought us out of the loop.
You can also see that in while loop there is no statement inside the while loop which can make the while loop expression False so if we did not use break there, it would give us an infinite loop.
And always remember to use the break keyword with if statement or else there is no use of loop if we directly inject the break keyword.
continue keyword in Python:
continue is a keyword used on the loops to skip the next upcoming code or statement of the loop and throw us again on the top of the loop.
Loop works on a repetitive sequence and continue keyword help us to skip some repetitive sequence.
Like break keyword, it would be great practice if we use continue keyword with the if statements, unlike break, continue does not terminate the loop.
for value in sequence: # code for for block if condition: continue #code for for loop #outside of for loop while expression: #code for while loop if if_expression: continue #code for while loop #outside of while loop
Let’s understand it with an example:
print("print odd number from 1 to 10 using for loop and continue ") for i in [1,2,3,4,5,6,7,8,9,10]: if i % 2 == 0: continue print(i)
print odd number from 1 to 10 using for loop and continue 1 3 5 7 9
In the above example, we have used continue with if statement and you can see that whenever the if statement executes the continue execute too which skip the print statement of while.
You can say that there is a default hidden continue keyword present in every loop which makes it perform the repetitive task again and again.
Points to remember:
- Always use break and continue keywords with if
- break helps us to terminate the loop and throw us out of the loop before its actual termination point
- continue helps to skip all the statements of the loop which are written below it.
Object & Class
- Python Iterators
- Python Generators
- Python Closures
- Python Decorators
- Python Shallow and Deep Copy
- Python Property
- Python Assert