Python if break | Example code

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 loop

Python if break function without loop error

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

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

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