When I started my programming journey just over a year ago, one of the concepts that baffled me was the difference between break, pass and continue. They aren’t too difficult to understand, but I kept having to refer online when writing code.

Lets discuss what these statements mean along with implementations in python (v3.6).

$ \ $

Break:

This is the statement used to exit a loop. This statement is very useful in that it allows the programmer to “do” something once a certain condition is met or else continue with whats going on. Let’s take a simple example. The function guessing_game() starts a game session where the user has to guess a randomly generated number. The user has an indefinite amount of attempts and the game gets over once the correct number is guessed.

import random

def game():
	to_guess = random.randrange(1, 5, 1)
	
	while True: # an infinite while loop
		user_input = int(input("Please enter a number")) 
		# the input() return value is typecasted to 
		# int because it returns a string
		if user_input == to_guess:
			print("Game over")
			break
	
game()

Let’s give it a try. Running it in python shell:

:~$ python3 main.py
Please enter a number4
Please enter a number3
Please enter a number2
Game over

As soon as the correct value was guessed, we exited from the loop and terminated the function.

Nested Loops

What happens when there are nested loops? Consider the example given below. There are 2 loops, each one goes from 0 to 2. When j is 1, the break statement is triggered.

for i in range(3):
    print("i =", i)
    for j in range(3):
        print("j =" , j)
        
        if j == 1:
            print()
            print("***")
            break

The output:

$ python3 main.py
i = 0
j = 0
j = 1

***
i = 1
j = 0
j = 1

***
i = 2
j = 0
j = 1

***

The conclusion: A break statement only exits the loop in which it is present.

$ \ $

Continue

This statement is very similar to the break statement in that it allows the programmer to use loops in better ways. The difference is that instead of terminating the loop (the pass statement does that), the continue statement just terminates the current iteration of that loop. Any code in the loop below the continue statement is never executed.

Consider the code below. It a simple loop that runs for three iterations. It prints the iteration number. A part of the code is after the continue statement used in the loop.

for j in range(3):
    print("loop =" , j + 1)
    
    print("before continue")
    continue
    print("past continue")

The output:

:~$ python3 main.py
loop = 1
before continue
loop = 2
before continue
loop = 3
before continue

Clearly, the continue statement breaks the current iteration of the loop and carries on with the next iteration.

$ \ $

Pass

This statement does n’t seem to have much functionality at first glance, but it really is an important part of python code. The pass statement basically functions like a placeholder. It allows you to run a part of the code even when you are not finished with the entire code. This will be easier to understand with a simple example. Consider a class PassExample, with 3 methods:

  • print_hi()
  • print_bye()
  • print_hello()

Suppose you have implemented the first two methods. But, you are not so sure if they work as expected. Since all three methods are quite similar, it would be better to check if this method works before typing out the entire code only to find that the code does n’t work.

class PassExample:
	
	def print_hi(self):
		print("hi")
		
	def print_bye(self):
		print("bye")
		
	def print_hello(self):
		

object = PassExample()
object.print_hi()
object.print_bye()

If you leave the print_hello() method blank, you get:

:~$ python3 main.py
  File "main.py", line 13
    object = PassExample()
    ^
IndentationError: expected an indented block

You should put a pass statement after the function declaration statement for print_hello() function to do the above correctly.

def print_hello(self):
	pass

The output:

~:$ python3 main.py
hi
bye

Now that you know the code works as expected, you can implement the print_hello() method and remove the pass keyword. Of course, even if you don’t remove the pass statement its not a big deal, but it makes your code look untidy.

The pass statement can also be used when a code-block is supposed to remain empty but is mentioned for readibility.

An example:

while True:
	if (x == 3)
		break
	else
		pass

The advantage of the pass statement over break and continue is that it can be used everywhere: in functions, classes, loops, conditional statements, except blocks…

Hopefully, usage of these statements is clear.