Dev ❤ Ops

Break and Continue Statement in Python

Break and continue statement In Python is used to exit a loop early and to skip the rest of the current iteration and move on to the next one respectively.

Here is an example of how to use break and continue in a loop:

for i in range(10):
    if i == 5:
        break
    print(i)

for i in range(10):
    if i % 2 == 0:
        continue
    print(i)

The first loop will print the numbers 0 through 4, and then exit the loop when it reaches 5. The second loop will print the numbers 1, 3, 5, 7, and 9 because it will skip the current iteration and move on to the next one if i is even.

It’s important to note that break and continue can only be used inside loops (for and while). Attempting to use them outside of a loop will result in a syntax error.

For more information please visit python.org

This article is created based on experience but If you discover any corrections or enhancements, please write a comment in the comment section or email us at contribute@devopsforu.com. You can also reach out to us from Contact-Us Page.

Follow us on LinkedIn for updates!

Leave a comment

Your email address will not be published. Required fields are marked *