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:
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.

Code file as described in the video

while(True):
    inp = int(input("Enter a Number\n"))
    if inp>100:
        print("Congrats you have entered a number greater than 100\n")
        break
    else:
        print("Try again!\n")
        continue
#i =0


#while (True):
#if i+1<5:
# i = i + 1
# continue

# print(i+1, end=" ")
# if(i==44):
# break
# i = i + 1
while (True):
inp = int(input("enter your number \n"))
if inp>100:
print ("congrats enter greater number \n")
break
else:
print ("print again number \n")
continue



Comments

Popular posts from this blog

HOW IMPORT WORKS IN PYTHON?

Exercise 5: Health Management System

*args and **kwargs In Python