In this Python tutorial, you will learn how the break statement stops a for loop or while loop immediately. You will also see how break behaves in nested loops and in loops that have an else block.
What Is the break Statement in Python?
The Python break statement terminates the nearest enclosing loop before all its planned iterations have finished.
When Python executes break, program control moves to the first statement after that loop. Any remaining iterations are skipped.
The break statement can be used only inside a for loop or a while loop. Using it outside a loop produces a SyntaxError.
A common use of break is to stop searching after a required value has been found, exit an input loop after a valid response is received, or end an otherwise indefinite loop when a condition becomes true.
Python break Statement Syntax
The syntax of the Python break statement is:
break
The statement normally appears inside an if block so that the loop stops only when a specific condition is satisfied.
for item in iterable:
if condition:
break
Python break in a for Loop
In the following example, the break statement is used inside a Python For loop. The loop prints numbers until it encounters the value 6.
Python Program
for number in [1, 2, 3, 5, 6, 10, 11, 12]:
if number==6:
break
print(number)
print('Bye')
Output
1
2
3
5
Bye
When number becomes 6, Python executes break. The values 6, 10, 11, and 12 are not printed. Execution then continues with print('Bye').
Python break in a while Loop
In the next example, the break statement stops a Python While Loop when the counter reaches 6.
Python Program
i=1
while i < 11:
if i==6:
break
print(i)
i=i+1
print('Bye')
Output
1
2
3
4
5
Bye
The loop ends before printing 6. The increment statement is also skipped for that iteration because it appears after break.
Using break to Stop a Search
The break statement is useful when a loop searches for one matching item. After the match is found, checking the remaining items is unnecessary.
Python Program
names = ['Arun', 'Meera', 'Ravi', 'Kiran']
target = 'Ravi'
for name in names:
if name == target:
print('Name found')
break
Output
Name found
Once Ravi is found, the loop ends without checking Kiran.
Python break in an Infinite while Loop
A while True loop continues indefinitely unless its body exits through break, return, an exception, or program termination. The following loop stops when number reaches 3.
number = 1
while True:
print(number)
if number == 3:
break
number += 1
print('Loop ended')
Output
1
2
3
Loop ended
How break Works in Nested Python Loops
In nested loops, break terminates only the innermost loop that contains it. The outer loop continues with its next iteration.
for row in range(1, 4):
for column in range(1, 4):
if column == 2:
break
print(row, column)
Output
1 1
2 1
3 1
For every value of row, the inner loop stops when column becomes 2. The outer loop itself is not terminated.
To stop multiple nested loops, you can use a flag variable, place the loops in a function and use return, or redesign the logic so that one condition controls the outer loop.
Python break with a Loop else Block
Python allows an else block after a for or while loop. The else block runs only when the loop finishes normally. It does not run when the loop is terminated by break.
numbers = [4, 7, 12, 15]
for number in numbers:
if number % 2 == 0:
print('First even number:', number)
break
else:
print('No even number found')
Output
First even number: 4
Because the loop executes break, the else block is skipped. This pattern is useful when searching for a match and handling the no-match case separately.
Difference Between break, continue, and pass
breakterminates the current loop completely.continueskips the remaining statements in the current iteration and begins the next iteration.passperforms no operation. It is commonly used as a placeholder where Python requires a statement.
Use break when no more iterations are needed. Use continue when only the current iteration should be skipped.
SyntaxError When break Is Used Outside a Loop
In the following example, break appears inside an if statement but not inside a loop. An if statement alone does not provide a valid context for break.
Python Program
i=1
if i==2:
break
print('Bye')
Output
File "example1.py", line 4
break
^
SyntaxError: 'break' outside loop
Python raises SyntaxError: 'break' outside loop because there is no enclosing for or while loop to terminate.
Common Python break Statement Mistakes
- Placing break outside a loop: An
ifstatement, function, or class does not makebreakvalid unless it is also inside a loop. - Expecting break to stop every nested loop: It stops only the nearest enclosing loop.
- Writing required work after break: Statements later in the same loop iteration are unreachable after
breakexecutes. - Forgetting the loop else behavior: A loop’s
elseblock is skipped when the loop ends throughbreak. - Using break when continue is required: Use
continueto skip one iteration without ending the entire loop.
Python break Statement FAQs
What does break do in Python?
The break statement immediately terminates the nearest enclosing for or while loop. Execution then continues with the first statement after the loop.
Can break be used inside an if statement?
Yes, but the if statement must itself be inside a for or while loop. An if statement outside a loop cannot contain a valid break.
Does break stop all nested loops?
No. It stops only the innermost loop that directly contains the break statement.
Does a loop else block run after break?
No. The else block associated with a loop runs only when the loop completes without executing break.
Summary of Python break
The Python break statement stops the nearest enclosing loop immediately. It can be used in both for and while loops, including indefinite loops and nested loops. After the loop ends, execution continues with the next statement following the loop.
In this Python Tutorial, you learned how to use the Python break statement, how it interacts with nested loops and loop else blocks, and why using it outside a loop causes a syntax error.
TutorialKart.com