Trusted answers to developer questions
Trusted Answers to Developer Questions

RELATED TAGS

What are break, continue, and pass statements in Python?

Hope Olaidé Wilson

Overview

Any program’s architecture (simple or complex) requires the exploration of a variety of possible coding solutions. To achieve the program functionality we desire, it is important that we check the suitability of the possible solutions.

The break, continue, and pass statements in Python let us move from one feature of the program to another. We also use them to test different areas of our code within the compiler while it is still under development.

The break statement

The break statement has the following characteristics:

  • Its syntax is in lower case.
  • It is used within a conditional expression.
  • It is indented by four spaces, or a single tab, according to PEP8 style guidelines for Python.

It lets us:

  • Restrict the number of times we loop or have a specific condition to end the loop.
  • Break out of a for or while loop.
Given a specified exception, the break statement allows one to exit a loop early

Let’s consider the following examples:

# Example of a `break` statement in a `for` loop:

fruit = ["apple", "orange", "banana", "pear", "mango"]

for i in fruit:
    print(i)
    if i == "pear":
        print("You found a pear!")
        break
Early exit of a for loop under a specified condition using the break keyword
#Example of a `break` statement in a `while` loop:

count = 0
while count < 12:
    count += 1
    print(count)
    if count == 8:
        print("End of program.")
        break 
Early exit of a while loop under a specified condition using the break keyword

The continue statement

The continue statement has the following characteristics:

  • Its syntax is in lower case.
  • It is used within a conditional expression.
  • It is indented by four spaces, or a single tab, according to PEP8 style guidelines for Python.

It lets us:

  • Continue looping or performing a task. Thus, we continue the looping cycle regardless of a particular condition that may have previously ended the loop.
Given a specified exception, the continue statement allows one to remain within the looping cycle

Let’s consider the following example:

# Example of a `continue` statement in a `for` loop:

for i in range (12):
    if i % 2 == 0:  
        continue 
    print(i)
In this example, only the odd numbers will print in the terminal because all even numbers will loop again (due to the continue statement) and skip the related print statement.
# Example of a `continue` statement in a `while` loop:
variable = 0

while variable < 12:
    variable += 1
    if variable % 2 == 0:
        continue 
    print(variable)
This example uses a while loop to give a similar result where only odd number values are printed. Once again, it is because the even numbers will continue the loop and skip the related print statement.

The pass statement

The pass statement has the following characteristics:

  • Its syntax is in lower case.
  • It is used within a function or conditional expression.
  • It is indented by four spaces, or a single tab, according to PEP8 style guidelines for Python.

The pass statement is a nullifying statement, meaning it terminates any operations that precede it.

The pass statement is particularly relevant for exploring the architecture of a function or a series of functions. It doesn’t execute anything beyond it, and it prevents the execution of anything that precedes it.

Test the incomplete function below:

def new_function():
    pass
The incomplete code above runs without producing any errors because the pass statement nullifies the code preceding it. The pass statement serves as a placeholder making it possible to return to the code and modify it for use when needed.

The break, continue, and pass statements in Python allow greater control and flexibility in coding by permitting useful exceptions to a program's architecture.

RELATED TAGS

RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time

Copyright ©2022 Educative, Inc. All rights reserved.

soc2