Skip to content

Break & Continue (break, continue)

Usage

1. continue

  • It can use for both for loop and while loop.
  • continue: will terminate/skip this round of looping
py
for i in range(1, 100):
    print(i)
    continue
    print(i) # Code is structurally unreachable
  • The code under continue will never reached and execute.

2. break

  • break: will stop the entire looping

Example

1. continue

loop-continue-break.py

py
for i in range(1, 5):
    if i == 2:
        continue # Skipped
    print(i)

Results: (Skipped 2)

1
3
4

Info

  • If nested looping structure, using continue will only affect the looping at the level of structure where continue is placed.

2. break

loop-continue-break.py

py
for i in range(1, 5):
    if i == 2:
        break # end
    print(i)

Results: (when i = 2, stop entire loop)

1