BREAK AND CONTINUE STATEMENT IN PYTHON
Break & Continue Statements In Python
“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:
Output:
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:
Output:
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.
Comments
Post a Comment