Skip to content

Latest commit

 

History

History
39 lines (33 loc) · 654 Bytes

File metadata and controls

39 lines (33 loc) · 654 Bytes

break vs continue

Purpose

Both break and continue are used to alter the default logic flow of both for and while loops.

break

Use break if you want to stop execution of the loop and proceed to the next line outside of the loop.

for char in 'coding':
    if char == 'n':
        break
    print(char)

### prints:
# c
# o
# d
# i

continue

Use continue if you want to immediately move on to the next iteration of the loop, without finishing the logic of the current iteration.

for char in 'coding':
    if char == 'n':
        continue
    print(char)
### prints:
# c
# o
# d
# i
# g