Introduction
Using for loops and while loops in Python allows you to repeat tasks automatically and efficiently. These loops are fundamental constructs in Python that allow you to iterate over sequences such as lists, tuples, and strings, or to execute a block of code repeatedly based on a condition.
However, there are scenarios where you need more control over the flow of your loops. For example, you might encounter a situation where you need to exit a loop prematurely, skip the current iteration, or simply have a placeholder for future code. Python provides three powerful statements to handle these cases: break, continue, and pass.
- The break statement allows you to exit a loop completely if a specific condition is met, effectively stopping the loop's execution.
- The break statement allows you to exit a loop completely if a specific condition is met, effectively stopping the loop's execution.
- The pass statement is a null operator. It is used as a placeholder in loops, functions, classes, or conditionals where code is syntactically required but you have nothing to execute.
Understanding and using these statements can significantly enhance your ability to manage loop control flow, making your code more efficient and easier to read. In the following sections, we will look at practical examples of how to use the break, continue, and pass statements in Python loops.
Prerequisites
You must have Python 3 installed and a development environment on your computer or server. Let's say you haven't set up a development environment. In that case, you can refer to the installation and setup guide to set up a local Python development environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.).
Break Statement
In Python, the break statement allows you to exit a loop when an external condition is triggered. You place the break statement in the block of code below your loop statement, usually after an if conditional statement.
Let's look at an example that uses the break statement in a for loop:
number = 0
for number in range(10):
if number == 5:
break # break here
print('Number is ' + str(number))
print('Out of loop')The variable number in this small program is initialized to 0. Then, if the variable number is less than 10, it creates a for loop.
In a for loop, an if statement provides a condition that the loop will break if the variable number is equal to the integer 5. For more information on using the for loop, you can refer to this tutorial on using the for() loop in Python.
Inside the loop there is also a print() statement that is executed with each iteration of the for loop until the loop breaks, because it is after the break statement.
Let's put a final print() statement outside the for loop so we know when we're out of the loop.
When you run this code, you will get the following output:
Output
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Out of loopThis shows that the loop breaks when the integer evaluates to 5, as the program is told to do with the break statement.
The break statement causes a program to exit a loop.
Continue Statement
The continue statement allows you to skip the part of a loop where an outer condition is triggered, but continue the loop. The current iteration of the loop is interrupted, but the program returns to the top of the loop.
The continue statement will be placed in the code block below the loop statement, usually after an if conditional statement.
Using the same for loop program in the break statement section above, we will use the continue statement instead of the break statement:
number = 0
for number in range(10):
if number == 5:
continue # continue here
print('Number is ' + str(number))
print('Out of loop')The difference in using the continue statement instead of the break statement is that our code continues despite the interruption when the variable number evaluates to 5. Let's examine our output:
Output
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Number is 6
Number is 7
Number is 8
Number is 9
Out of loopHere, the number 5 never occurs in the output, but the loop continues after that point to print lines for the numbers 6-10 before exiting the loop.
You can use the continue statement to avoid deeply nested conditional code or to optimize a loop by eliminating items you want to skip.
The continue statement causes a program to skip certain elements that appear in a loop but then continue through the rest of the loop.
Pass Statement
When an external condition is triggered, the pass statement allows you to handle the condition without affecting the loop in any way. All code continues to read unless a failure or other statement occurs.
Like other statements, the pass statement will be placed in the code block below the loop statement, usually after an if conditional statement.
Using the same code block above, let's replace the break or continue statement with a pass statement:
number = 0
for number in range(10):
if number == 5:
pass # pass here
print('Number is ' + str(number))
print('Out of loop')After the if conditional statement, the pass statement tells the program to continue executing the loop and ignore that the variable number evaluates to 5 during one of its iterations.
You run the program and get the following output:
Output
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9
Out of loopBy using the pass statement in this program, you will notice that the program executes exactly as it would if the conditional statement were not present in the program. The pass statement tells the program to ignore the condition and continue executing the program as usual.
The pass statement can create minimal classes, or act as a placeholder when working on new code and thinking at an algorithmic level before hammering out the details.
Result
The break, continue, and pass statements in Python allow you to use for and while loops more effectively in your code.









