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.
break
statementThe break
statement has the following characteristics:
It lets us:
for
or while
loop.# 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 = 0while count < 12:count += 1print(count)if count == 8:print("End of program.")break
continue
statementThe continue
statement has the following characteristics:
It lets us:
# Example of a `continue` statement in a `for` loop:for i in range (12):if i % 2 == 0:continueprint(i)
# Example of a `continue` statement in a `while` loop:variable = 0while variable < 12:variable += 1if variable % 2 == 0:continueprint(variable)
pass
statementThe pass
statement has the following characteristics:
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.
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
CONTRIBUTOR