Python continue, break and pass Statements


In this tutorial you will learn about advanced topics in control flow – how to use Python Continue Break and Pass statements.


Python continue break passs Statements


Using a for loop and a while loop in Python allows you to repeat tasks in an efficient manner.

Sometimes you may want the task to skip a single iteration or even exit the loop. You can do this actions with continue, break and pass statements.

Python continue Statement

Using Python continue statement you can skip the current iteration, and continue to the next one. The current iteration of the loop will be disrupted, but the program will return to the top of the loop. You can use this statement both in for and while loops. The continue statement can be useful when you want the loop skip over an iteration when a specific condition is triggered.

For instance, suppose you want to write a script that prints the results of division of some numbers. If for some reason one of the divisors is “zero”, an exception will be raised. One way to avoid this problem is by using a suitable continue statement. Watch the example:

# continue dividend = 1 for i in range(-3, 3): if i==0: print("Can't divide by 0") continue print("{} divided by {} equals to {}".format(dividend, i, dividend / i))

Flow Chart of Python continue

Flow Chart of Python continue statement

Python break Statement

Using Python break statement, you can terminate the current loop (not just one iteration). After it, the execution resumes to the next part of the code. A break statement can be used both in for and while loops. It can be useful when you want the entire loop to terminate when a specific condition is triggered.

For instance, suppose you write a for loop that finds the first item that is bigger than 5 in a list. A break statement can be useful in this case. Here is an example:

# break my_list = [1, 4, 5, 7, 9, 3, 5, 6] for i in my_list: if i > 5: print("The first item bigger than 5 is {}".format(i)) break

Flow Chart of Python break

Flow Chart of Python break statement


The else Statement Used with Loops After a break Statement

The else statement lets you execute code when a for loop ends. If the for loop is terminated using a break statement, the else statement won’t be executed. Watch the example:

# else my_list = [1, 4, 5, 7, 9, 3, 5, 6] for i in my_list: if i > 5: print("The first item bigger than 5 is {}".format(i)) break else: print("executed?") # won't be executed

Python continue Nested Loop

Some cases might require using a continue statement on an outer loop, inside the code block of an inner loop. It is best to avoid those situations. This coding style is considered to be “bad practice”, as it is complex and not very readable. That's why it is best to refactor your code and think about a better way to implement the task. However, if you wish to continue the outer loop inside a nested loop you can do it by using a break statement inside the inner loop.

Here is an example:

# nested continue dividend = 1 my_list = [1, 4, 5, 7, 9, 3, 5, 6] for i in my_list: for k in my_list: if k == 4: break

Python pass Statement

Some statements in Python shouldn't remain empty. For instance, an empty block below an if statement raises an error. The pass statement is a null statement - it does nothing. It is generally used as a placeholder. When you don't want to write right now the code, you can use pass.

# pass for i in range(10): pass print('Success')

Python - continue vs pass

Even though at first the continue and pass statements might seem similar, they do completely different things. The continue statement skips the current iteration of a loop whereas pass does nothing at all. The pass statement is a placeholder for code. The example below can be useful to understand the differences:

# continue vs pass my_list = [1, 2, 3] for i in my_list: if i == 2: continue print(i) # doesn't print 2 for i in my_list: if i == 2: pass print(i) # prints all the numbers

Exercise

Here is a list of non-negative integers from 0 to 100. Use the continue and break statements to print all the numbers that:

  1. Can't be divided by 7
  2. Not bigger than 80

# Exercise my_list = list(range(100)) # Your code goes here: