EFFECTIVE BREAK, CONTINUE AND PASS STATEMENTS IN PYTHON – 7

BREAK, CONTINUE AND PASS IN PYTHON | INNOVATE YOURSELF
1
0

OVERVIEW

In this blog, you will learn,

  • What are break, continue and pass statements?
  • How to use break, continue and pass statements with loops and how it affects the functionality of a loop?

Now, let’s understand all about the break, continue and pass.

What are break, continue and pass statements?

In python, break, continue and pass, has a special functionality that can change the way how we handles the loop for finite or infinite loops. If you have and infinite loop than we can terminate that loop with break statement, or if we have a finite or infinite loop and we want to skip any case based on the condition then we skip that case with continue statement. If we have a many cases and we have to perform various actions on other conditions except one or more condition. In that case we call pass statement to do nothing.

Here, are the flow diagrams of break and continue,

WORKING OF BREAK, CONTINUE AND PASS

Now, we’ll understand that how to use these statements with loops in python. Firstly let’s understand how to use break statement. To understand this, let’s take a scenario where we have written a program that will print all the values of a given string,

message=”Innovate Yourself”

But we have a condition that if the printed value id “Y” then loop should be terminated, in this case we’ll use break statement.

BREAK

Let’s write the program and understand it better.

message = "Innovate Yourself" 
for alphabet in message:
    if alphabet == "Y":
        break
    print(alphabet)

For the above example, the loop will terminate if the value of alphabet will be “Y”. Following is the output for the given code

output for break statement

CONTINUE

Now let’s write the similar program for the above case using continue statement. The output will be quite similar the difference will be that instead of terminating the loop continue statement will skip that case where the condition got satisfied and will continue for the rest of the iterators.

message = "Innovate Yourself"
for alphabet in message:
if alphabet == "Y":
continue
print(alphabet)

Here is the code. Now as per this code when the value of alphabet will be “Y” instead of skipping rest of the cases and terminating it, it will skip the condition (i.e., alphabet==”Y”) and continue for rest of the cases. The output as per continue statement is as follows.

continue out CONTINUE AND PASS

PASS

Now, let’s write the program to understand how does the pass statement works and where it is used. Here is the code for above case with pass statement.

message = "Innovate Yourself"
for alphabet in message:
if alphabet == "Y":
pass
print(alphabet)
only pass CONTINUE AND PASS

As per the above code, if you will run the code then you will notice that it will print every character of the string. That means the pass statement doesn’t perform anything. So why do we use pass statements if it doesn’t do anything?

To make you clear about pass statement let’s write another program where we’ll have multiple conditions and as per the condition if the value of alphabet is in lower case then we don’t have to do anything otherwise will have to use break and continue if value of alphabet is “Y” and “I” respectively.

message = "Innovate Yourself"
for alphabet in message:
if alphabet.lower() == alphabet:
pass
elif alphabet == "Y":
break
elif alphabet == "I":
continue
print(alphabet)

Now as per the above case if the character is in lower case then pass statement will be executed and the expression will be printed and if the character is not lower case then we have two other conditions that is alphabet == “Y” and alphabet == “I” that will terminate and skip the current condition respectively.

pass CONTINUE AND PASS

Also check this video for more clarification,

Time to wrap up now. Hope you liked our content on using break, continue and pass with loops in python. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more python programming contents.

Also check out our other playlist Rasa Chatbot, Internet of things, Docker, Python Programming, etc.
Become a member of our social family on youtube here.

Leave a Reply