In this Python tutorial, you will learn about two keywords, namely break & continue . Also, you will understand how and where to use them. So, let's start discussing Python continue and break Keywords.
What are break and continue Keywords in Python?
The break and continue are two special keywords in Python that are often used with for and while loops 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 becomes False or we reach the end of the sequence? Well, in such a case, we need some keywords that can alter the functionality of loops.
Break Keywords in Python
The break is a keyword often used with while and for loops to terminate their repetitive functionality. The moment the for loop finds the break keyword inside its body, it stops working and throws us out of the loop. In general, the break keyword is used to counter the loop, so if you are using a nested loop (a loop inside a loop), the break will only counter the loop that defines it. break Keyword Syntax
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 the following 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 affects the loop and brought us out of the loop. You can also see that in the while loop, there is no statement that can make the while loop expression False . So, if we did not use break there, it would give us an infinite loop. Always remember to use the break keyword with the if statement or else there is no use of the loop if we directly inject the break keyword.
Continue keywords 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 the continue keyword helps us to skip some repetitive sequences. Like the break keyword, it would be great practice if we use the continue keyword with the if statements. Unlike the break keyword, continue does not terminate the loop. continue Keyword Syntax:
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 the continue keyword with the following 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 the continue keyword with the if statement. and you can see that whenever the if statement gets executed, the continue keyword executes too, which skips the print statement of the while loop. Also, we can say that there is a default hidden continue keyword present in every loop which allows the loop to perform the repetitive task again and again.
Following are the key points to remember about the break and continue keywords:
- Always use the break and continue keywords with the if statement.
- The break keyword helps us to terminate a loop and throw us out of the loop before reaching the actual termination point.
- The continue keyword helps to skip all the statements of a loop that are written below it.