Python break and continue

In this Python article, we will discuss the use of break and continue keywords for altering the loop flow in any python program.

Loops work efficiently but some conditions may arise when we want to exit a running loop or skip some iterations. We can achieve this by loop control statements.

Loop control statements make the execution of loops work in a different way than it works in normal conditions.

1. How do break and continue work?

Generally, a loop is executed till the time the test condition is true, but somehow we can terminate the execution or any current iteration and there will be no need to check the test expression or condition.

For example, suppose we have a list of all the students with a course and we want to find details of a specific student, in such scenarios with generally start with looping over the whole list of objects, and stop as soon as we find the required object as we do not have to iterate over the rest of the list once we have found the needed one. In such scenarios break keyword is used.

And in another scenario, we might want to skip the details of all the students from a particular batch. In such scenarios, we can use the continue statement to skip the details of all the students that match that certain criteria.

Here comes in pictures two useful statements in python.


2. break statement in Python

break is simply used to terminate the loop. If we use break in the loop’s body, the control of the program will execute the statements after the body of the loop after the break keyword is encountered.

Syntax:
break
Python Break statement flow diagram
Python Break statement flow diagram

Let’s take an example to understand the working of break. In this program, the for loop will stop further execution of loop when word == 8.

# An example for break
list = [5, 6, 7, 8, 9]
for word in list:
  if word == 8:
    print("Break encountered")
    break
  print(word)
Output
5
6
7
Break encountered

Note: If the break is used in a nested loop then it will break the nested loop only, there will be no effect on the outer loop.


3. continue statement in Python

If we want to skip the remaining lines of the code in the loop after a particular condition is fulfilled then we can use the continue statement inside the loop.

Syntax:
continue
Python Continue statement flow diagram
Python Continue statement flow diagram

continue will not end the execution of the loop but it will go to the next iteration.

Let’s implement a program to use the continue keyword which will skip the rest of the loop block when word == 8.

list = [5, 6, 7, 8, 9]
for word in list:
  if word == 8:
    print("Skipping the 8")
    continue
  print(word)
Output
5
6
7
Skipping the 8
9

Note: If the continue is used in a nested loop then it will skip the nested loop iteration only, there will be no effect on the outer loop.


4. Conclusion

In this article we learned about

  • Break statement in python
  • continue statement in python

We can also implement break and continue for while loops. Just give it a try and comment on your experiments with it.


Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index