All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Difference Between Break And Continue Statement In Python

Last Updated : Mar 11, 2024

Difference Between Break And Continue Statement 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)
  1. We will use two for loops to demonstrate the difference between a break statement and a continue statement.
  2. 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.
  3. The for loop will iterate through the range of 1 to 10. In the next line, there is an if condition.
  4. 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.
  5. 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.
  6. The break statement can be used when some external condition is triggered and requires the termination of the loop.
  7. 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.
  8. 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.
  9. 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.
  10. 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.
  11. 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.
  12. The continue statement can be used when we don’t want to terminate the loop and just want to skip iteration.
  13. In this case, the print statement will display the values 0 to 9 except for the value 2 in the output window.

Conclusion :-

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.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪