Break & Continue (break, continue)
Usage
1. continue
- It can use for both
for loopandwhile 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
continuewill 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
4Info
- If nested looping structure, using
continuewill only affect the looping at the level of structure wherecontinueis 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