Break statements exist in Python to exit or “break” a for or while conditional loop. When the loop ends, the code picks up from and executes the next line immediately following the loop that was broken.

numbers = (1, 2, 3)
num_sum = 0
count = 0
for x in numbers:
        num_sum = num_sum + x
        count = count + 1
        print(count)
        if count == 2:
                break

In this example, the loop will break after the count is equal to 2.

The continue statement is used to skip code within a loop for certain iterations of the loop. After the code is skipped, the loop continues where it left off.

for x in range(4):
   if (x==2):
      continue
   print(x)

This example would print all numbers from 0-4 except 2.

 

Leave a Reply

Your email address will not be published. Required fields are marked *