Python if break | Example code

  • by

Generally, the break keyword is used in the if statement inside the loop to break it. You can’t “break” an if statement. You can “break” the while loop though. You can add an “if” statement inside the while loop to break it

Python if break using example code

Simple example code.

Python break for loop

Using break with if statement conditions.

number = 0

for count in range(10):
    if count == 2:
        break  # break here

    print('count ' + str(count))

print('Out of loop')

Output:

Python if break

break function Python

Can’t use it: SyntaxError: ‘break’ outside the loop

Python if break function without loop error

Do comment if you have any doubts or suggestions on this Python if statement with break keyword.

Note: IDE: PyCharm 2021.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.

This site uses Akismet to reduce spam. Learn how your comment data is processed.