Python break Statement
breakIt is a keyword in Python used to immediately terminate a loop.
When the program executes tobreakit immediately breaks out of the current loop structure (whether for or while), and no longer executes the remaining code in the loop.
Word Meaning: breakIt means "break, interrupt", used to terminate loops.
Basic Syntax and Parameters
breakIt is a standalone statement and does not require any parameters.
Syntax Format
while 条件:
if 退出条件:
break
# 其他代码
Usage Scenarios
- Early exit: Exit the loop immediately when a specific condition is met.
- Search termination: Exit the search after finding the target.
- Infinite loop exit: Use together with while True, and exit via break.
Examples
Example 1: Using in a for loop
Example
# Find the first even number in the list
numbers = [1, 3, 5, 6, 7, 8, 9]
for num in numbers:
if num % 2 == 0:
print(f"Found the first even number: {num}")
break
print("Search complete")
numbers = [1, 3, 5, 6, 7, 8, 9]
for num in numbers:
if num % 2 == 0:
print(f"Found the first even number: {num}")
break
print("Search complete")
Expected output:
找到第一个偶数: 6 搜索结束
Code explanation:
- Traverse the list and exit immediately after finding the first even number.
- No longer continue traversing the remaining elements.
Example 2: Using in a while loop
Example
# Accumulate until it exceeds 100
total = 0
i = 1
while True:
total += i
if total > 100:
print(f"When i = {i}, the sum first exceeds 100")
break
i += 1
print(f"Total: {total}") # Output: Total: 105
total = 0
i = 1
while True:
total += i
if total > 100:
print(f"When i = {i}, the sum first exceeds 100")
break
i += 1
print(f"Total: {total}") # Output: Total: 105
Expected output:
When the sum exceeds 100, exit the loop immediately.
Example 3: break in Nested Loops
Example
# Nested loop: break only exits the inner loop
for i in range(1, 4):
for j in range(1, 4):
if i == 2 and j == 2:
break # Only exits the inner loop
print(f"({i}, {j})", end=" ")
print() # Newline
for i in range(1, 4):
for j in range(1, 4):
if i == 2 and j == 2:
break # Only exits the inner loop
print(f"({i}, {j})", end=" ")
print() # Newline
Expected output:
(1, 1) (1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2) (3, 3)
Code explanation:
- break only exits the innermost loop that contains it.
- The outer loop continues to execute.
Example 4: Using with else
Example
# break causes else to not execute
for i in range(5):
if i == 3:
print("Target found, exiting early")
break
print(i)
else:
print("Loop completed normally, target not found")
print("---")
# Normal end (no break)
for i in range(5):
print(i)
else:
print("Loop completed normally, else is executed")
for i in range(5):
if i == 3:
print("Target found, exiting early")
break
print(i)
else:
print("Loop completed normally, target not found")
print("---")
# Normal end (no break)
for i in range(5):
print(i)
else:
print("Loop completed normally, else is executed")
Expected output:
0 1 2 找到目标,提前退出 --- 0 1 2 3 4 循环正常结束,else 执行
In a for-else or while-else structure, break causes the else block not to execute.
Other Extensions
Python3 Loop Statements