break and continue statement in Python
In this session, we will see the use and functionality of break and continue statement in Python. ‘break’ and ‘continue’ are the statement that are used to change the course of ‘for’ and ‘while’ loops. In other words, you can control the execution of these loops using break and continue. They are the Python reserved keywords and you cannot use them for naming user defined variables or functions.
break – There might be some situation where you want to exit the loop immediately and proceed with the instruction immediately after the loop. This might be useful when your loop hits some of your defined matching criteria and you don’t want the loop to be executed further.
continue – you might want to stop the current execution of the loop statement and start the loop all over at the start from the next value of the control variable. This will be useful when your loop hits some unwanted criteria and hence for that value, you don’t want the remaining loop instruction to be executed. Hence you jump to the start of the loop with next value of control variable.
Let’s see below the use of break and continue statement in Python.
Example of break keyword in Python:
1. Use of break statement – here the criteria to exit the loop is val==3. We are printing val starting from 0. As soon as val becomes 3, we exit the for loop and print “The end”
for val in range(5): if val == 3: break print(val) print("The end")
2. Use of break statement – here our criteria is i==3. As soon as value of i become 3, control check the if condition, go inside if statement, print the line and then breaks out of the for loop and proceed from statement after for loop
for i in range(5): print("Inside 'for' loop. Value of i is,",i) if i==3: print("Inside 'if' statement. I will break now \n") break print("I did not went inside 'if' statement \n") print("Outside 'for' loop")
3. break using nested if statement – In this example, we have just added one more level of if statement to see how does break behaves. You will notice that print statement ‘Outside second if statement’ will never be executed. Because this statement is inside first if statement.
As soon as the value of i becomes 3, the control will go inside first if and then the control will go to second if and then the flow will hit break, so the flow will go out of for loop itself
for i in range(5): print("Inside 'for' loop. Value of i is,",i) if i==3: print("Inside 'if' statement. Check more condition") if i<4: print("Inside second 'if' statement. I will break now \n") break print("Outside second 'if' statement") print("I did not went inside 'if' statement \n") print("Outside 'for' loop")
4. break keyword terminates only the immediate loop in which break is mentioned – If break is present in nested loops, then control will terminate immediate loop in which if statement is present and will go the loop higher than the immediate loop where break is present.
In the below example, when the break is encountered, only the inner for loop is terminated and the control moves to higher for loop
for i in range(3): print(“inside first for loop.Value of i is: “,i) for j in range(2): print(“Inside second for loop.Value of j is: “,j) if i==1 and j==1: print(“Inside if”) break print(“Outside second for loop”) print(“Loops completed”)
5. break statement in while loop – here our criteria to exit the loop is var==5. We will start printing from 10 and as soon as var becomes 5, we exit the loop and print “Loop ends”
var=10 while var>1: if var==5: break print(“Var value is:”,var) var-=1 print(“Loop ends”)
6. break statement in while loop – We have used nested while loop. Run the code and try to understand the logic
i=3 j=3 while i>=1: print(“Inside firt while loop. Value of i is: “,i) while j>=1: print(“Inside second while loop. Value of j is: “,j) if i==j: print(“Inside if statement”) break j-=1 i-=1 print(“Outside second while loop”) print(“Outside first while loop”)
Example of continue statement in Python:
7. use of continue statement – here our criteria is i==3. As soon as value of i becomes 3, the for loop stops current execution and start from i==4.
for i in range(5): if i==3: print("Inside if statement") continue print("Inside for loop. ",i) print("Outside for loop")
8. use of continue in nested for loops – just like break, here also the control goes to the immediate loop in which if statement is present.
for i in range(3): print("Inside outer for loop. ",i) for j in range(3): if i==1 and j==1: print("Inside if statement") continue print("Inside inner for loop. ",j) print("\n") print("Outside for loop")
9. use of continue – while loop
i=4 while i>=1: if i==2: i-=1 continue print("Inside while loop.",i) i-=1 print("Outside while loop")
For More Details: https://realpython.com/lessons/using-break-and-continue/https://realpython.com/lessons/using-break-and-continue/