Python Glossary Русский

all Python 3Loopsconstruct

break

control

Statement that leaves the nearest enclosing loop immediately. If the loop has an else block, it is NOT executed after a break.

Syntax

break

break leaves only the single innermost loop; you can't jump straight out of several nested loops — use a flag, refactor into a function with return, or raise an exception. It pairs naturally with a loop's else: the else runs only if the loop finished without ever hitting break — the classic 'not found' idiom.

Examples

for i in range(10):
    if i == 5:
        break
print(i)  # → 5
lst = [1, 3, 7, 4, 9]
for x in lst:
    if x % 2 == 0:
        print("первое чётное:", x)  # → первое чётное: 4
        break
found = False
target = 7
for x in lst:
    if x == target:
        found = True
        break
print(found)  # → True
i = 0
while True:
    i += 1
    if i >= 3:
        break
print(i)  # → 3

See also

Open in the full glossarydocs.python.orgReport an inaccuracy