Tutorial

Python Break & Continue Statement

6 min read

In this article, we are going to discuss the break & continue statements in Python. The break statement allows a programmer to exit a loop early, while the continue statement allows a programmer to skip an iteration of a loop.

These statements can be incredibly useful in certain situations and can help to improve the efficiency and readability of our code. In this post, we will be diving deep into the usage and practical applications of these statements and providing examples to help explain their functionality.

Python Break Statement

The break statement in Python is used to exit a loop early before the loop has completed all of its iterations. This can be useful in situations where a certain condition has been met and there is no need to continue iterating through the loop.

Python Break Statement with For Loop

Here is an example of how the break statement can be used in a for loop

for number in my_list:
    if number % 2 == 0:
        print(f"The first even number is {number}")
        break

Here, the loop iterates through the list my_list and checks each number to see if it is even. If an even number is found, the loop exits early and the number is printed.

Note: It’s important to note that the break statement only exits the innermost loop in which it is used. If we want to exit multiple loops, we can use a loop control variable or flag variable along with the break statement.

Python Break Statement with While Loop

The break statement in Python can also be used within a while loop to exit the loop early. A while loop repeatedly executes a block of code as long as a certain condition is true.

The break statement can be used to exit the loop when a certain condition is met before the loop’s condition becomes false.

Example of Python break statement in a while loop-

i = 0
while i < 10:
    print(i)
    if i == 5:
        break
    i += 1
# output: 0 1 2 3 4 5

Here, the loop starts with the variable i being 0 and will continue to execute as long as i is less than 10.

The loop will print the current value of i and then increment i by 1. Once the value of i is equal to 5, the loop will exit early using the break statement. This means that the code inside the loop will stop being executed and the program will continue to run the next line of code after the while loop.

Note: It’s important to be careful when using the break statement inside a while loop, as it can be easy to create an infinite loop if the condition that causes the loop to exit is never met. This can lead to the program running indefinitely and consuming a lot of resources.

Python Continue Statement

The continue statement in Python is used to skip the current iteration of a loop and move on to the next iteration. This can be useful in situations where we want to skip certain iterations based on a certain condition.

Python Continue Statement with For Loop

Example of continue statement used in a for loop:

for number in my_list:
    if number % 2 == 0:
        continue
    print(number)

Here, the loop iterates through the list my_list and checks each number to see if it is even. If an even number is found, the current iteration is skipped using the continue statement and the program moves on to the next iteration of the loop. If the number is odd, it is printed.

Note: It’s important to note that the continue statement only skips the current iteration of the loop in which it is used. It doesn’t exit the loop or change the control flow of the program.

Python Continue Statement with While Loop

The continue statement in Python can also be used within a while loop to skip the current iteration and move on to the next one. A while loop repeatedly executes a block of code as long as a certain condition is true. The continue statement can be used inside the loop’s block to skip the current iteration when a certain condition is met.

Example of continue statement in Python while loop-

i = 0
while i < 10:
    i += 1
    if i % 2 == 0:
        continue
    print(i)
#output: 1 3 5 7 9

Here, the loop starts with the variable i being 0 and will continue to execute as long as i is less than 10. The loop will increment i by 1 and check if i is an even number using the modulo operator. If the number is even, the loop will skip the current iteration using the continue statement and move on to the next one. If the number is odd, it will print the current value of i. This way, the loop will only print the odd numbers from 1 to 9.

Note: It’s important to remember that when the continue statement is executed, the code below it will not be executed for that iteration. Also, the continue statement does not affect the loop’s condition, so the loop will continue executing as long as the condition is true.

Python Continue Statement with Nested Loops

The continue statement can also be used within nested loops in Python, allowing us to skip certain iterations of inner loops based on a certain condition. A nested loop is a loop that is inside another loop, and it allows you to iterate over multiple sequences at once.

Example:

for i in range(3):
    for j in range(3):
        if j == 1:
            continue
        print(i, j)
#output: 
0 0
0 2
1 0
1 2
2 0
2 2

Here, the outer loop iterates over the range 0 to 2 using the variable i, and the inner loop iterates over the range 0 to 2 using the variable j. The inner loop has an if statement that checks if the value of j is 1. If it is, the current iteration of the inner loop is skipped using the continue statement and the program moves on to the next iteration of the inner loop. If the value of j is not 1, then the program prints the current values of i and j.

Note: When using continue statements within nested loops, it’s important to be mindful of the indentation, as it should be inside the inner loop. Otherwise, it will skip the outer loop iteration instead of the inner one.

Conclusion

In conclusion, the break and continue statements in Python are important tools for controlling the flow of loops.

The break statement is particularly useful when we need to exit a loop early once a certain condition is met. It can be used in both for and while loops and it helps to avoid infinite loops and unnecessary iterations.

The continue statement, on the other hand, is useful when we want to skip certain iterations of a loop based on a certain condition. It allows us to continue iterating through a loop without performing certain actions during certain iterations. It can also be used in both for and while loops.

They can help to improve the efficiency and readability of our code by allowing us to exit a loop early or skip certain iterations based on a certain condition.

It is important to use them with caution and test the code to ensure that the loop behaves as expected.