View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Python Break, Continue & Pass Statements [With Examples]

By Rohit Sharma

Updated on Oct 06, 2022 | 6 min read | 6.3k views

Share:

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: 

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

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!

background

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree18 Months
View Program

Placement Assistance

Certification8-8.5 Months
View Program

Frequently Asked Questions (FAQs)

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

2. What is the difference between continue and pass statements?

3. Where are these statements used in Python?

Rohit Sharma

697 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

upGrad Logo

Certification

3 Months

View Program
Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree

18 Months

View Program
IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in Data Science & AI

Placement Assistance

Executive PG Program

12 Months

View Program