MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python Break & Continue Statements


“Break and continue statements are used to alter the flow or normal working of a loop, that could be either a “for loop” or “while loop”.

If you are not familiar with the concept of loops, I recommend you go through Tutorial numbers 16 and 17 of Python Tutorials For Absolute Beginners In Hindi Playlist.

You all know what break and continue mean in the basic English language. Break means interrupt and continue means resuming after an interrupt as the meanings of the words can describe that both these functions are totally opposite to each other. Hence, both of these keywords are mostly defined together, like “if and else” or “try and except,” but unlike the others, their functionality is quite the opposite of each other. They aren’t even used together in most of the cases like “except”, could only be used if there is “try” or “else” condition and if there isn’t “if” statement present, but in cases of “break and continue", they both do not have any such relation. They may be defined together but not mostly used together.
Defining break statement, break statement alters the normal functionality of the loops by terminating or exiting the loop containing it. The compiler then moves on to the code that is placed after the body of the loop. The syntax of break is only a single word, i.e., “break”. A break statement is relatively easier to understand than a continue statement as it just leaves the bunch of code written after it inside the loop. The control of the program is then shifted to the statement written after the loop.

Example Of Break Statement:

i=0;
while(True):
    print(f"The value of i is : {i}")
    i=i+1
    if(i>10):
        print("Breaking the loop. ")
        break;

Output

The value of i is : 0
The value of i is : 1
The value of i is : 2
The value of i is : 3
The value of i is : 4
The value of i is : 5
The value of i is : 6
The value of i is : 7
The value of i is : 8
The value of i is : 9
The value of i is : 10
Breaking the loop.


Continue statement also alters the flow of a normal working loop, but unlike the break statement, it takes the compiler to the start of the code that has already been executed before the statement but is inside the loop boundary. All the code written after the continue statement is skipped, but it is worth noting that the continue statement works only for a single iteration. Unless in situations where it's written with decision statements such as if, else, etc., in those situations, the continue statement will totally be dependent upon the condition of the decision statement. Its syntax is also plain and easy, like a break statement as you only have to use the keyword “continue”.

Example Of Continue Statement:
i=0;
while(True):
    i=i+1
    if(i==5):
        continue
    if(i>10):
        break
    print(f"The value of i is : {i}")

Output

The value of i is : 1
The value of i is : 2
The value of i is : 3
The value of i is : 4
The value of i is : 6
The value of i is : 7
The value of i is : 8
The value of i is : 9
The value of i is : 10


Where and when can these statements come in handy:



When you are working with a big project, there might occur a situation where you only need to use the loop partially without adding new or removing already existing lines of code. You can easily apply the break statement in your code so that you can partially run it without any sort of error.

Or in another situation, let's suppose you are working with while loop printing some lines on the screen and you want to skip an iteration in between others, then you can write the continue statement using an “if” statement that matches your need so that you can skip the specific iteration.

Conclusion

In this page (written and validated by ) you learned about Python Break & Continue Statements . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Operators.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.