What Is A Break, Continue, And Pass Statement In Python?

What Is A Break, Continue, And Pass Statement In Python?

Published on October 01 2023

In this tutorial, we will learn what break, continue, and pass statements in Python and will understand them with an example in detail. This tutorial is part of Python's top 25 most commonly asked interview questions.

Break

  • This statement helps terminate the loop or the statement when some condition is met and pass the control to the next statement.

Sample Code

num = 5
for i in range(0,num):
    if i == 2:
        break
    print(i)
It will print 0,1 and break the loop

Continue

  • The continue statement terminates the current iteration of the statement, skips the rest of the code in the current iteration and the control flows to the next iteration of the loop.

Sample Code

num = 5
for i in range(0,num):
    if i == 2:
        continue
    print(i)
It will print 0,1,3, 4 and skip 2

Pass

  • This is basically a null operation. Nothing happens when this is executed. 
  • Used when you need some block of code syntactically, but you want to skip its execution.

Sample Code

def print_message():
    pass
    
print_message()
nothing will be printed

To know about it, please refer to the official documentation website - official website.