Difference Between Break And Continue Statement In Python
Last Updated : Jan 1, 2023
IN - Python
In this tutorial we will show you the solution of difference between break and continue statement in python, break and continue statements are also known as “Loop Control Statements”, as they automate the working of the inner loop.
The loop automates the task and repeats it in a certain manner, but there are some special cases where we need to terminate the loop or skip some iterations.
There we use a Loop Control Statement that is Break and Continue. So, let’s get started.
Step By Step Guide On Difference Between Break And Continue Statement In Python :-
Python supports three basic loop control statements - Break Statement, Continue Statement, Pass Statement
In this article we will only see the difference between the break statement and the continue statement.
for i in range(10): if i == 6: break else: print(i) print("\n") for i in range(10): if i == 2: continue else: print(i)
- We will use two for loops to demonstrate the difference between a break statement and a continue statement.
- In the first line there is a for loop which has a variable named “I”, which will be used as the iterating variable in the for loop.
- The for loop will iterate through the range of 1 to 10. In the next line, there is an if condition.
- This if condition checks if the value of the variable “I” is equal to 6 or not. If the condition is true, then the break statement will be executed.
- The break statement terminates the current working loop and passes control to the next statement; if the break statement resides inside the nested loop, it passes control to the outer loop.
- The break statement can be used when some external condition is triggered and requires the termination of the loop.
- In the else part of the if condition, there is a print statement that will display the value of “I” in each iteration. In this case, it will only display from 0 to 5.
- In the next line, there is a print statement that will print the “\n” newline character, which will be displayed as a blank line in the output window. This line is optional.
- In the next line there is another for loop, which is similar to the above for loop with the slight difference that it has a continue statement.
- In the if condition, it checks whether the value of the variable “I” is equal to 2 or not. If it is, then the continue statement will be executed, and if the condition is false, then the print statement will be executed.
- The continue statement works in opposition to the break statement. Instead of terminating certain conditions, it jumps off to the very next condition. But it will continue the execution of the loop statement as per its name.
- The continue statement can be used when we don’t want to terminate the loop and just want to skip iteration.
- In this case, the print statement will display the values 0 to 9 except for the value 2 in the output window.
So finally, in conclusion, we can say that with the help of this article, you have the basic knowledge of the difference between a break statement and a continue statement in Python.
You can say that the break statement leaves a loop, whereas the continue statement jumps to the next iteration.
I hope this tutorial on difference between break and continue statement in python helps you and the steps and method mentioned above are easy to follow and implement.