Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python break statement

  • Difficulty Level : Medium
  • Last Updated : 22 Nov, 2019

Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

  • Continue statement
  • Break statement
  • Pass statement

In this article, the main focus will be on break statement.

Break statement

Break statement in Python is used to bring the control out of the loop when some external condition is triggered. Break statement is put inside the loop body (generally after if condition).





# Python program to
# demonstrate break statement
s = 'geeksforgeeks'
# Using for loop
for letter in s:
    # break the loop as soon it sees 'e'
    # or 's'
    if letter == 'e' or letter == 's':
print("Out of for loop")
i = 0
# Using while loop
while True:
    # break the loop as soon it sees 'e'
    # or 's'
    if s[i] == 'e' or s[i] == 's':
    i += 1
print("Out of while loop")


Out of for loop

Out of while loop

In the above example, both the loops are iterating the string ‘geeksforgeeks’ and as soon as they encounter the character ‘e’ or ‘s’, the if condition becomes true and the flow of execution is brought out of the loop.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!