Loops in Python: Using the break, continue, and pass statements

This post is lesson 13 of 54 in the subject Python Programming Language

1. Using the break statement in python

The break statement in python is used to immediately terminate the loop that contains it.

Example of the break statement in a for loop

sum = 0
for i in range(1, 7):
    if(i!=5):
        sum = sum + i
    else:
        break
    print(i, end=' ')
print("\nsum = ", sum)

Result

1 2 3 4 
sum =  10

Example of the break in python in a while loop

i = 1
sum = 0
while i < 7:
    if(i!=5):
        sum = sum + i
    else:
        break
    print(i, end=' ')
    i = i + 1
print("\nsum = ", sum)

Result

1 2 3 4 
sum =  10

If the break statement in python is located within a nested loop (a loop inside another loop), the break statement will terminate the innermost loop.

for i in range(1, 4):
    for j in range(4, 7):
        if j == 6:
            break;
        print(i, "*", j, "=", i*j)

Result

1 * 4 = 4
1 * 5 = 5
2 * 4 = 8
2 * 5 = 10
3 * 4 = 12
3 * 5 = 15

In the above example, the i variable will have the values 1, 2, and 3 and j will have the values 4, 5, 6. But with j==6, the break statement is executed and the loop containing it (the innermost loop) is exited. Therefore, the result will not have the multiplication of i with 6.

2. Using the continue statement in python

The continue statement is used to skip the remaining part of the code inside the loop for the current iteration. The loop doesn’t end and will continue with the next iteration.

Example of the continue statement in a for loop

sum = 0
for i in range(1, 7):
    if(i==5):
        continue
    sum = sum + i
    print(i, end=' ')
print("\nsum = ", sum)

Result

1 2 3 4 6 
sum =  16

In the above example, when i==5, the continue statement will execute, and the remaining statements in the for loop of the iteration where i==5 will not execute. Therefore, the result 5 will not be displayed on the screen.

Example of using the continue statement in a while loop

i = 1
sum = 0
while i < 7:
    if(i==5):
        i = i + 1
        continue
    sum = sum + i
    print(i, end=' ')
    i = i + 1
print("\nsum = ", sum)

Result

1 2 3 4 6 
sum =  16

3. Using the pass statement in python

The pass statement is empty. When the pass statement is executed, nothing happens.

The pass statement is often used when programmers don’t know what code to write. Sometimes, the pass is used when we don’t want any code to execute. The pass statement can be placed in a loop, function definition, class definition, or an if statement.

Example of using a pass statement in a loop:

n = 10
for i in range(n):
  # pass can be used as placeholder
  # when code is to added later
  pass

li =['a', 'b', 'c', 'd']

# Print b c d only
for i in li:
    if(i =='a'):
        pass
    else:
        print(i)

An example of using the pass keyword within a function or class

def myFunction():
    pass
class myClass:
    pass

Example of the pass command in an if statement

a = 10
b = 20
 
if(a < b):
    pass
else:
    print("b < a")

The pass statement is only available in Python. Other languages such as C/C++, Java, etc. do not have the pass statement.

5/5 - (1 vote)
Previous and next lesson in subject<< While loop control structure in PythonHow to Define a Function in Python? >>

Leave a Reply

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