Python - break & continue

python logo pixel art

Python `break` and `continue` keywords are used to control the flow of loops.



while condition:
  # tasks...
  if condition:
    break

for i in iterable:
  # tasks...
  if condition:
    break
while condition:
  # tasks...
  if condition:
    continue

for i in iterable:
  # tasks...
  if condition:
    continue

while True: # <- infinite-loop
  x = input('Type a number:')
  
  if x == 'quit' or not x.isnumeric():
    break
    
  if x.isnumeric():
    print(int(x) * 2)
num = [4, 53, -3, 47, -87]
result = 0

for n in num:
  if n < 0:
    continue
    
  result += n