Trusted answers to developer questions

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

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

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 breakstatement

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
#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

The continuestatement

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)
# Example of a `continue` statement in a `while` loop:
variable = 0
while variable < 12:
variable += 1
if variable % 2 == 0:
continue
print(variable)

The passstatement

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 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

python
edpressocompetition
Did you find this helpful?