Python Exit For Loop with Example Programs

Programmers use for loops in Python to efficiently rerun their program until they acquire the exact series of outputs.

Occasionally, an external element may affect the execution of your application.

When this occurs, you may want your program to perform the following mitigations:

  • Exit the loop entirely
  • Skip a portion of the loop before proceeding
  • Disregard the external element

To perform these operations, you may apply Python’s break, continue, and pass statements.

Break Statement Python

The Python break statement allows you to exit a loop whenever an external condition is triggered.

Place the break statement within the block of code beneath your loop statement, often following an if statement.

Example:

number = 0

for number in range(10):
    if number == 5:
        break    # break here

    print('Number is ' + str(number))

print('Out of loop')

Output:

Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Out of loop

Explanation:

This example shows how to terminate the loop function within a program.

A for statement is then used to construct the loop if the variable number is less than 10.

Within the for loop, there is an if statement, which offers the condition that the loop breaks if the variable number is equal to the integer 5.

Since the print() command follows the break a statement, it will execute with each iteration of the for loop until the loop terminates.

By adding a final print() statement outside of the for loop, you will determine when the loop has been completed.

This shows that the break instruction tells the computer to get out of the loop when the integer is equal to 5.

Python Continue Statement

In Python, the continue statement allows you to bypass the portion of a loop where an external condition is met while continuing to execute the remainder of the loop.

So, the current loop iteration will stop, but the program will go back to the start of the loop.

The continue statement will be in the code block that comes on top of the loop statement.

This is usually after an if statement that checks a condition.

The difference between utilizing the break and continue statements is that the continue statement proceeds regardless of the interruption caused by the variable number evaluation.

Example:

number = 1

for number in range(10):
    if number == 3:
        continue

    print(number)

print('Out of loop')

Output:

0
1
2
4
5
6
7
8
9
Out of loop

Explanation:

This example executes the for loop and initiates output production.

The output shows that the program skipped the number 3 and kept running the for loop until it reached the end of the loop’s range.

This example justifies that the continue statement lets you avoid deeply buried conditional code.

Moreover, it can improve a loop by taking out common cases that you want to reject.

Therefore, the continue statement tells a program to skip certain iteration factors in a loop and move on to the rest. 

Pass Statement in Python

With Python’s pass statement, you can handle an outside condition without making any changes to the loop.

All of the code will continue until a break or another statement happens.

As with the other statements, the pass statement will be in the block of code under the loop statement.

Example:

number = 0

for number in range(5):
    if number == 2:
        pass

    print(number)

print('Out of loop')

Output:

0
1
2
3
4
Out of loop

Explanation:

The pass statement usually comes after the if statement.

It tells the program to keep running the loop even if one of its iterations shows that the variable is equal to 2.

Using a pass statement reveals that it executes similarly to how it would if there were no conditional statements.

Moreover, the pass statement instructs the program to disregard the condition and continue as usual.

It can be used to build simple classes or as a placeholder, while working with new code.

Furthermore, the statement proceeds at an algorithmic level before figuring out the details.

Summary

In summary, this tutorial completes the technique on how the Python exit for loop functions with examples.

Additionally, this article also gives other mitigations that will help you skip or continue with your program.

It is possible by using continue and pass statements.

These statements help you identify the kind of functions that you may apply to your program to proceed with your ideas.

Therefore, this tutorial lets you explore more of Python’s conditional statements and functions.

Leave a Comment