Skip to content

Python continue vs break vs pass

  • by

Python continue vs break vs pass have very minor differences but logically make a higher difference. Python provides break used to exit a loop completely and continue statements skip a part of the loop and start the next execution.

Python pass statement is used as a placeholder inside loops, functions, class, and if-statement that is meant to be implemented later.

continuebreakpass
The continue statement skips only the current iteration of the loop.The break statement terminates the loop.The pass statement is used to write empty code blocks.
We can use the continue statements only inside a loop.We can use break statements only inside a loop.We can use pass statements anywhere in the Python code.

The break Statement

# First Example
for letter in 'Hello':
    if letter == 'e':
        break
    print('Current Letter :', letter)

# Second Example
var = 5
while var > 0:
    print('Current variable value :', var)
    var = var - 1
    if var == 2:
        break

print("Good bye!")

Output:

Python continue vs break vs pass

The continue Statement:

# First Example
for letter in 'Hello':
    if letter == 'e':
        continue
    print('Current Letter :', letter)

# Second Example
var = 5
while var > 0:
    print('Current variable value :', var)
    var = var - 1
    if var == 2:
        continue

print("Good bye!")

Output:

Current Letter : H
Current Letter : l
Current Letter : l
Current Letter : o
Current variable value : 5
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Good bye!

The pass Statement:

for letter in 'Python':
    if letter == 'h':
        pass
        print('This is pass block')
    print('Current Letter :', letter)

print("Good bye!")

Output:

Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!

Do comment if you have any doubts or suggestions on this Python basic topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *