Python break is used to terminate the execution of the loop.
Python break statement Syntax:
Loop{
Condition:
break
}
Python 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). It terminates the current loop, i.e., the loop in which it appears, and resumes execution at the next statement immediately after the end of that loop. If the break statement is inside a nested loop, the break will terminate the innermost loop.
Example of Python break statement
Example 1:
Python
for i in range(10):
print(i)
if i == 2:
break
Output:
0
1
2
Example 2:
Python
# Python program to
# demonstrate break statement
s = 'geeksforgeeks'
# Using for loop
for letter in s:
print(letter)
# break the loop as soon it sees 'e'
# or 's'
if letter == 'e' or letter == 's':
break
print("Out of for loop" )
print()
i = 0
# Using while loop
while True:
print(s[i])
# break the loop as soon it sees 'e'
# or 's'
if s[i] == 'e' or s[i] == 's':
break
i += 1
print("Out of while loop ")
Output:
g
e
Out of for loop
g
e
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’, if the condition becomes true and the flow of execution is brought out of the loop.
Example 3:
Python
num = 0
for i in range(10):
num += 1
if num == 8:
break
print("The num has value:", num)
print("Out of loop")
OutputThe num has value: 1
The num has value: 2
The num has value: 3
The num has value: 4
The num has value: 5
The num has value: 6
The num has value: 7
Out of loop
In the above example, after iterating till num=7, the value of num will be 8 and the break is encountered so the flow of the execution is brought out of the loop.
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 some statements of the loop before continuing further in the loop. These can be done by loop control statements called jump statements. Loop control or jump 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/jump statements.
Python break statement – FAQs
What is a break statement in Python?
A break
statement in Python is used to terminate the current loop prematurely when it’s encountered. It immediately stops the iterations and exits the loop. It’s commonly used in for
and while
loops to halt the execution when a specific condition is met.
for i in range(10):
if i == 5:
break
print(i)
# Output: 0, 1, 2, 3, 4
Does Python break exit all loops?
No, the break
statement in Python only exits the nearest enclosing loop, not all loops. To exit multiple nested loops, break
must be used in each loop, or an alternative structure like raising an exception may be employed.
for i in range(3):
for j in range(3):
if j == 1:
break
print(f"i = {i}, j = {j}")
# Output shows the inner loop breaks when j equals 1.
What is the difference between break and continue statement in Python?
break
Statement: Exits the loop entirely.continue
Statement: Skips the current iteration and moves to the next iteration of the loop.
for i in range(5):
if i == 3:
continue
print(i)
# Output: 0, 1, 2, 4 (skips 3)
Is break a return statement?
No, the break
statement is not a return statement. break
only exits a loop, while return
exits from a function entirely, optionally passing back a value.
What is the advantage of break statement?
The advantage of the break
statement is that it provides a way to stop loop execution based on a condition. This can make the program more efficient by avoiding unnecessary iterations and helps in controlling the flow of the program, especially in scenarios where an early termination condition is known.
# Searching for an element and stopping when found
elements = [1, 2, 3, 4, 5]
search_for = 3
found = False
for element in elements:
if element == search_for:
found = True
break
if found:
print(f"Found {search_for}")
else:
print(f"{search_for} not found")
# Output: Found 3
The break
statement in this context efficiently exits the loop once the target element is found, preventing unnecessary iterations over the remaining elements.