Pylerno
English

Loop control

Distinguish stopping a loop from skipping an iteration.

On this page

The concept

break exits the nearest enclosing loop. continue skips the remaining body of the current iteration and moves to the next one. pass does nothing; it exists where syntax requires a statement. These operations are not interchangeable.

A loop's else block runs if the loop completes without break, including when there were no iterations. This is convenient for searches: finding a result can break, while else reports absence. Its indentation associates it with the loop, not the if inside that loop.

Worked example

python
for number in range(1, 8):
    if number == 3:
        continue
    if number == 6:
        break
    print(number)
for number in [2, 4, 6]:
    if number % 2 != 0:
        print("An odd number was found")
        break
else:
    print("No odd number was found")
if True:
    pass

Trace the result

  1. When number is 3, continue prevents its print call; when it is 6, break stops the loop entirely.
  2. The second loop looks for an odd value but finds none, so its else block runs.
  3. The final pass changes no state and does not stop execution.
text
1
2
4
5
No odd number was found
Line-by-line walkthrough

Lines 1–6: for number in range(1, 8):

Obtain each item from range(1, 8) and bind it to number before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.

Lines 2–3: if number == 3:

Evaluate number == 3. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.

Line 3: continue

Skip the rest of this iteration and proceed to the next loop cycle; this does not terminate the loop.

Lines 4–5: if number == 6:

Evaluate number == 6. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.

Line 5: break

Stop the nearest enclosing loop immediately. Its else block will not run for this exit.

Line 6: print(number)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Lines 7–12: for number in [2, 4, 6]:

Obtain each item from [2, 4, 6] and bind it to number before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.

Lines 8–10: if number % 2 != 0:

Evaluate number % 2 != 0. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.

Line 9: print("An odd number was found")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 10: break

Stop the nearest enclosing loop immediately. Its else block will not run for this exit.

Alternative or completion suite: read the statements below as part of this branch.

Line 12: print("No odd number was found")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Lines 13–14: if True:

Evaluate True. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.

Line 14: pass

Perform no operation. pass fills a syntactically required block and does not stop the program.

Common mistake

In nested loops, break exits only the nearest loop. Do not assume it terminates the entire surrounding operation.

Check your understanding

What does this program print?

python
for n in [2, 4, 6]:
    if n == 4: break
    print(n)
Choose one answer

Search documentation

Share this page