Break Statement

The break statement is used to terminate the for or while loop.

We should use break statement along with the if statement.




Example

Given a list of numbers, print all numbers until we hit the number zero.

numbers = [1, 2, 3, 0, 5, 6, 7]
 

Output

1

2

3




Python Script

Example

#Break statement example

numbers = [1, 2, 3, 0, 5, 6, 7]

for i in numbers:
 if(i == 0):
   break
 print(i)





Continue Statement

Continue statement is used in looping to skip some statements.

We should use continue statement along with the if statement.

When continue statement encounters, the execution will go to the starting of the loop.

The statements below continue statement will not be executed if the given condition becomes true.

Example

Given a list of numbers, print all numbers except the number 0.

numbers = [1, 2, 3, 0, 5, 6, 7]
  

Output

1

2

3

5

6

7




Python Script

Example

#continue statement example

numbers = [1, 2, 3, 0, 5, 6, 7]

for i in numbers:
  if(i == 0):
    continue
  print(i)


Topics You Might Like