Python Break Statement
In Python, loops are used to automate and repeat processes in an effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the condition. Loop control statements can be used to do this. Control statements in loops alter the execution sequence. All automatic objects generated in scope are deleted when execution exits that scope. The following control statements are supported by Python.
- Continue statement
- Break statement
- Pass statement
In this article, the main focus will be on break statement.
When an external condition is triggered, the break statement in Python is used to pull the control out of the loop. Inside the loop body is a break statement (generally after if condition).

When the loop finishes due to iterable exhaustion (with for) or when the condition becomes false (with while), but not when the loop is ended by a break statement, the else clause is performed. The following loop, which looks for prime numbers, is an example of this:
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print(n, 'equals', x, '*', n//x)
... break
... else:
... # loop fell through without finding a factor
... print(n, 'is a prime number')
Output:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
When used with a loop, the otherwise clause is more similar to the else clause of a try statement than it is to the else clause of if statements: the else clause of a try statement runs when no exception happens, while the else clause of a loop runs when no break occurs.
Let us see a few examples to understand the concept better.
# Python program to
# demonstrate break statement
s = 'JavaTPoint'
# Using for loop
for letter in s:
print(letter)
# break the loop as soon it sees 'v'
# or 't'
if letter == 'v' or letter == 't':
break
print("Out of for loop")
print()
i = 0
# Using while loop
while True:
print(s[i])
# break the loop as soon it sees 'v'
# or 't'
if s[i] == 'v' or s[i] == 't':
break
i += 1
print("Out of while loop")
Output:
J
a
v
Out of for loop
J
a
v
Out of while loop
Both loops in the preceding example iterate the string 'JavaTPoint,' and when they reach the character 'v' or 't,' the if condition is true, and the flow of execution is taken out of the loop.
Let’s look at one more example to understand the concept in a better way.
#!/usr/bin/python
for letter in 'Python': # First Example
if letter == 'h':
break
print 'Current Letter :', letter
var = 10 # Second Example
while var > 0:
print 'Current variable value :', var
var = var -1
if var == 5:
break
print "Good bye!"
Here is the output of the code:
Current Letter : P
Current Letter : y
Current Letter : t
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!