Python break for loop with Examples

In this article, we will learn how Python break for loop with the help of examples.

As we all know, using for loops and while loops enables you to automate and repeat processes efficiently; however,

Instances occur where external factors may impact the performance of our application. When this happens, you could do these things: exit the loop entirely, skip a portion of the loop before continuing, or ignore the external element when this occurs.

But with the use of break, continue, and pass statements, these operations can be executed.

With that being said, let’s discover how to break a loop in Python

How to break a for loop in python?

As mentioned above, for loops and while loops in Python automate and repeat codes efficiently.

But then, Python’s break statement lets you exit a loop when an external condition is met. The break statement should be inserted into the code block that comes after the loop statement, usually after an if statement condition.

Let’s look at an example of how to use a break statement in a for loop:

num = 0

for number in range(10):
    if number == 4:
        break

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

print('Out of loop')

In this sample program, variable num is set to 0. The loop is then created using a for statement if the variable num is less than 10.

Within the for loop, there is an if statement that states if the variable number is equal to the integer 4, the loop will break.

There is also a print() statement within the loop that will execute with each iteration of the for loop until the loop breaks since it occurs after the break statement.

Lastly, to identify when the loop is finished, we’ve inserted a final print() statement outside of the for loop.

Output of the example code:

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

What is the use of break and continue in Python?

The use of break and continue in Python is that their statements can change the typical flow of a loop.

While the main function of loops is to iterate over a code of block until the expression of the test is false. However, sometimes we want to stop the current iteration of the whole loop without checking the expression.

This is when the break and continue statement is used.

Break statement Python

The Break statement in Python will terminate the loop when executed. While the statement that comes immediately after the loop’s body is the one that receives and controls statements of the program.

On the other hand, nested loops (one loop inside another) will be terminated by the break statement if it is inside the innermost loop.

Syntax of break

break

Flowchart of Break

Example of  Break statement Python

for val in "python":
    if val == "x":
        break
    print(val)

print("The end")

Output

p
y
t
h
o
n
The end

Explanation

In this program, the “python” sequence is iterated. Upon determining that the letter is x, we exit the loop. Thus, we observe in our output that all letters up until x are written. Following this, the loop ends.

Python continue statement

The continue statement bypasses the remainder of the code within a loop solely for the current iteration. The Loop moves on to the following iteration without terminating.

Syntax of Python continue statement

continue

Flowchart of Python continue statement

Example

for val in "python":
    if val == "x":
        continue
    print(val)

print("The end")

Output

p
y
t
h
o
n
The end

Explanation

This program is the same as the above example, except the break statement has been replaced with continue.

We continue with the loop if the string is x, not executing the rest of the block. Hence, we see in our output that all the letters except x get printed.

Can you break a for loop in Python?

The break statement can be used in both while and for loops. If you are using nested loops, the break statement terminates the innermost loop execution and starts executing the next line of code after the block.

To learn more about Python, visit our website to see a bunch of Python tutorials that can definitely help you.

Does Break stop all loops?

The Python break statement immediately terminates the loop entirely.

Conclusion

In conclusion, a Python break for loop is crucial, especially when you want to terminate the loop when in instances affecting the program flow.

Along with the Python continue statement, which skips the remainder of the loops code and continues to the iteration without terminating the program.

Leave a Comment