Python · Stage 4 – Repeating Things · Lesson 26 of 50

break and continue

Stop a loop early or skip only its current iteration.

for number in [1, 2, 3]:
    if number == 2:
        break
    print(number)
Line-by-line explanation
  1. The loop begins with 1.
  2. When number becomes 2, the condition is True.
  3. break ends the loop.
  4. Only 1 is displayed.
for number in [1, 2, 3]:
    if number == 2:
        continue
    print(number)
Line-by-line explanation
  1. The loop visits each number.
  2. For 2, continue skips the print line.
  3. The output contains 1 and 3.