Break and Continue Statement

Last updated Aug 10, 2021

Break Statement

Break statements in python are used to terminate the current loop and resumes execution at the next statement. The main function of the Break statement in python is used to bring the control out of the loop when under any external condition is triggered to the function or loop. One can use Break Statement in for loop and while loop.

It is time to understand the use of Break Statement with the help of an example:-

for val in "programming":
    if val == "a":
        break
    print(val)
p
r
o
g
r    

From the above code, you can see that our code stopped working after the implementation of the Break Statement when the value of the val function reaches it executes the break statement.

Continue Statement

In Python, we use Continue Statement to skip the code which comes afters the continue keyword and there is a control in the statement which is passed back to the start for the next iteration. The major functionality of the Continue Statement is to returns the control at the beginning of the loop. Continue Statement can also be used in both for loop and while loop like Break Statement.

Let's understand the best example which will explain the use of Continue Statement:-

for i in range(10):
    if i == 7:
        continue
    print(i)
0   
1
2
3
4
5
6
8
9

As you can see from the above code, number 7 is missing in the output as we used the continue statement at number 7. So this number has skipped from the for loop which results in printing all the numbers which are used in the loop.

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

136 Views