The break and continue Statement with For Loop




The break Statement

With the break statement we can stop the loop before it has looped through all the items:


Example

Exit the loop when x is "two":


name = ["one""two""three"]
for x in name:
  print(x)
  if x == "two":
    break



The continue Statement

With the continue statement we can stop the current iteration of the loop, and continue with the next:


Example

Do not print two:


name = ["one""two""three"]
for x in name:
  if x == "two":
    continue
  print(x)

 


Post a Comment

Thanks for comment.

Previous Post Next Post