Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Science USbreadcumb forward arrow iconPython Break, Continue & Pass Statements [With Examples]

Python Break, Continue & Pass Statements [With Examples]

Last updated:
30th Aug, 2021
Views
Read Time
6 Mins
share image icon
In this article
Chevron in toc
View All
Python Break, Continue & Pass Statements [With Examples]

Structure Theorem suggests that any computer program can be written using a primary control structure. The flow of control, or control structure, is a block of programming that analyzes information, variables, and conditions. Based on that, decides the path or direction to take based on the given conditions or parameters. Simply put, control structures are just decisions that the computer makes. It is a decision-making process, and the flow of control determines how the program will respond to the various conditions and parameters thrown at it. 

Computer programming works with data and instructions. To get behind data, you need to understand the nuances of data types and variables, whereas to work with instruction, you need to understand control structures and the flow of control. There are three types of control structures for any program – sequential, selection, or repetition. Every programming language comes with a special set of statements that allow programmers to manually jump the flow of control from one point to another based on predefined conditions. 

In Python, the most commonly used control flow statements are Break, Continue, and Pass. This article will explain the nuances behind the three statements, along with the syntax and examples! 

Break Statement in Python

The Break statement in Python allows you to exit a loop when a particular condition is met or triggered. The Break statement is placed within the block of the loop statement, after a conditional “if” statement that you want to check before exiting the loop. Here is an example to understand this better: 

Ads of upGrad blog

Program:

num = 0

for num in range(10):

    if num == 5:

        break    # break here

    print(‘num is ‘ + str(num))

print(‘Loop exited’)

The value of num is initialized at 0. Then, a for loop iteratively increases the value of num in the range 0 to 10. However, we have a break statement that checks if the value of num is equal to five anywhere. When the value of num hits 5, our break statement forces the control to exit this loop and reaches outside the loop. 

To know whether or not we are out of the loop, we are executing a print statement outside the loop. If it is executed, it indicates we have exited the loop. The output of the above program is as follows

Output

num is 0

num is 1

num is 2

num is 3

num is 4

Loop exited

As you can see, the moment the value of num is evaluated as 5, the loop immediately breaks, and the control is passed on to outside of the loop. 

Continue Statement in Python

Continue statement allows you to skip past specific parts of loops where certain extra conditions are triggered. However, unlike Break, the Continue statement does not take the control out of the loop but lets the entire loop execution complete. Thus, the Continue statement disrupts the current iteration of the loop but doesn’t stop the program from executing. Instead, it returns control to the top of the loop. 

Continue statement is used within various loops, generally after a conditional statement for checking the triggering conditions. Using the same program as above, replacing break with continue, here is how the code looks: 

Program:

num = 0

for num in range(10):

    if num == 5:

        continue    # continue here

    print(‘num is ‘ + str(num))

print(‘Loop exited’)

By definition, in this case, our code will continue despite the disruption even when the value is equivalent to 5. Here is our output: 

Output:

num is 0

num is 1

num is 2

num is 3

num is 4

num is 6

num is 7

num is 8

num is 9

Loop exited

As you can see, the statement ‘num is 5’ is never printed because the moment our num variable assumes the value 5, the continue statement takes the control back to the top of the loop and doesn’t allow that particular print statement to get executed. This is why we never get “num is 5” in our output list. 

Continue statement is extremely helpful if you wish to avoid deeply nested conditional code or optimize loops by eliminating cases that you would like to reject. 

Learn data science courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Pass Statement in Python

On a particular external condition getting triggered, the Pass statement allows you to handle the condition without impacting the loop whatsoever. All of the code of the loop will be continued to be read and executed unless a break or other exit statement occurs. 

Similar to Break and Continue, the Pass statement is also used inside loops, typically after conditional statements. In the previous code, let us replace continue with pass and see what happens: 

Program:

num = 0

for num in range(10):

    if num == 5:

        pass    # pass here

    print(‘num is ‘ + str(num))

print(‘Loop exited’)

The pass statement mentioned after the if condition tells Python to continue executing the loop and not consider that the variable num is equivalent to 5 during one of the iterations. Here’s what the output of the above program comes to be: 

Output:

num is 0

num is 1

num is 2

num is 3

num is 4

num is 5

num is 6

num is 7

num is 8

num is 9

Loop exited

As you can see, using Pass statements in the program allows Python to run the program exactly as it would in the absence of any conditional statement. Basically, the pass statement tells the program to completely disregard the condition and run the program normally. 

Pass statements can come extremely handy while creating minimal classes or even act as placeholders while working on a new piece of code and thinking on the algorithmic level before getting to coding. 

To Conclude

Ads of upGrad blog

Knowing how to manipulate loops and control structures is a vital skill for any programmer, especially if you’re looking to work in the field of Data Science programs. Furthermore, you’ll get the upper hand over your competitors. Try to build code by yourself, mix and match the control flow statements, and figure out your code’s output. It will help solidify all that you’ve learned from this blog!

If, at any stage of your career journey, you feel caught up and stuck, know that upGrad is for you! We have helped students across 85+ countries and over 500,000 working professionals to upskill and gain industry knowledge. Whether you are from a programming background or from a non-tech background, Certificate Programme in Data Science is designed to keep everyone in mind. Reach out to us today, and witness a learning atmosphere that fosters growth, collaboration, networking, and 360-degree support!

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.
Get Free Consultation

Select Coursecaret down icon
Selectcaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Best Data Science Courses

Frequently Asked Questions (FAQs)

1 What are break, continue, and pass statements used for?

These are control flow statements available in Python that allow you to switch your program’s flow from one point to another based on predefined conditions and rules.

2What is the difference between continue and pass statements?

The continue statement gets executed before the control is passed on to the next section. Pass statement, on the other hand, skips the condition and passes the control to the next section without executing the conditions of the pass statement.

3 Where are these statements used in Python?

Generally you’ll be using break, continue, and pass statements while working with and manipulating different loops in Python.

Explore Free Courses

Suggested Blogs

Top 10 Real-Time SQL Project Ideas: For Beginners & Advanced
14561
Thanks to the big data revolution, the modern business world collects and analyzes millions of bytes of data every day. However, regardless of the bus
Read More

by Pavan Vadapalli

28 Aug 2023

Python Free Online Course with Certification [US 2024]
5519
Data Science is now considered to be the future of technology. With its rapid emergence and innovation, the career prospects of this course are increa
Read More

by Pavan Vadapalli

14 Apr 2023

13 Exciting Data Science Project Ideas & Topics for Beginners in US [2024]
5474
Data Science projects are great for practicing and inheriting new data analysis skills to stay ahead of the competition and gain valuable experience.
Read More

by Rohit Sharma

07 Apr 2023

4 Types of Data: Nominal, Ordinal, Discrete, Continuous
5886
Data refers to the collection of information that is gathered and translated for specific purposes. With over 2.5 quintillion data being produced ever
Read More

by Rohit Sharma

06 Apr 2023

Best Python Free Online Course with Certification You Should Check Out [2024]
5594
Data Science is now considered to be the future of technology. With its rapid emergence and innovation, the career prospects of this course are increa
Read More

by Rohit Sharma

05 Apr 2023

5 Types of Binary Tree in Data Structure Explained
5385
A binary tree is a non-linear tree data structure that contains each node with a maximum of 2 children. The binary name suggests the number 2, so any
Read More

by Rohit Sharma

03 Apr 2023

42 Exciting Python Project Ideas & Topics for Beginners [2024]
5753
Python is an interpreted, high-level, object-oriented programming language and is prominently ranked as one of the top 5 most famous programming langu
Read More

by Rohit Sharma

02 Apr 2023

5 Reasons Why Python Continues To Be The Top Programming Language
5326
Introduction Python is an all-purpose high-end scripting language for programmers, which is easy to understand and replicate. It has a massive base o
Read More

by Rohit Sharma

01 Apr 2023

Why Should One Start Python Coding in Today’s World?
5213
Python is the world’s most popular programming language, used by both professional engineer developers and non-designers. It is a highly demanded lang
Read More

by Rohit Sharma

16 Feb 2023

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon