break and continue Statement in Python
Loops in Python
In this lesson, we will understand what is break and continue statement in Python programming along with some examples.
What is break Statement in Python
The break statement in Python is a loop control statement which is used to terminate the loop. When the break statement encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement written outside the body of the loop.
The break statement is used in situations when we want to terminate the loop based on some condition.
Note: A break statement can be used in for loop and while loop.
We will see here the usage of break statement with three different types of loops:
- Simple for Loop
- Nested for Loop
- Infinite for Loop
Simple for Loop: Consider the situation where we want to exit from the loop when user enters a specific number.
Below is the example of using break with simple for loop:
for x in range(2, 50, 2): if x==10: break print(x, end=' ')
2 4 6 8
In the above example, we want to exit from the loop when the value of x is equal to 10. So, we have used a break statement inside the body of if statement which executes when the given condition is true, and the loop terminates.
Nested for Loop: We can also use break statement while working with nested loops. If the break statement is used in the innermost loop, the control will come out only from the innermost loop.
Below is the example of using break with nested for loop:
for x in range(1, 6): for y in range(1, 11): if y>3: break else: print(y, end=' ') print()
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
In the above example, we can clearly see that the inner loop is programmed to execute for 10 times. But as soon as the value of y becomes greater than 3 the inner loop stops executing which restricts the inner loop to run 3 times only. However, the iteration of the outer loop remains unaffected.
Therefore, break applies only to the loop within which it is present.
Infinite for Loop: The break statement can be included in an infinite loop with a condition to terminate the execution of the infinite loop.
Below is the example of using break with infinite for loop:
import itertools i=1 for x in itertools.repeat(1): if i>10: break print(i, end=' ') i=i+1
1 2 3 4 5 6 7 8 9 10
In the above example, the for loop executes an infinite number of times. To terminate the loop, we have used a break statement which terminates the loop, when the value of i is greater than 10.
What is continue Statement in Python
The continue statement in Python is also a loop control statement just like the break statement. The continue statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.
As the name suggests, the continue statement forces the loop to continue or execute the next iteration. When the continue statement executes in the loop, the code inside the loop following the continue statement will skip and the next iteration of the loop will begin.
Note: A continue statement can be used in for loop, while loop and do while loop.
Consider the situation when you need to write a program which prints the number from 1 to 10 except the number 6. It is specified that you have to do this using loop and only one loop is allowed to use.
Here comes the usage of continue statement. What we can do here is we can run a loop from 1 to 10, and every time we have to compare the value of iterator with 6. If it equals to 6, we will use the continue statement to continue to next iteration without printing anything, otherwise, we will print the value.
Below is the implementation of above idea:
# we run a loop from 1 to 11 for x in range(1, 11): # If the value of x is equals to 6, # continue to next iteration # without printing anything if x==6: continue; # otherwise print the value of x print(x, end=' ')
1 2 3 4 5 7 8 9 10
In the above example, when the value of x becomes 6, then continue statement execute and the loop move to the next iteration without executing anything written after the continue statement.