Python Break and Continue – Tutorial with Examples

In Python, the break and continue statements are used to control the flow of execution in loops. These statements allow you to alter the normal flow of a loop, either by skipping certain iterations or by terminating the loop entirely.

Break Statement

The break statement is used to exit a loop prematurely, before it has completed all of its iterations. Once a break statement is executed, the loop will stop executing, and the program will continue to execute the next statement after the loop.

For example, consider the following code that uses a for loop to find the first multiple of 7 in a list of numbers:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers:
    if number % 7 == 0:
        print("Found a multiple of 7:", number)
        break

The output of the above code will be:

Found a multiple of 7: 7

In this example, the loop continues to iterate through the list of numbers until it finds the first multiple of 7. Once it finds the multiple, it prints the result and then uses a break statement to exit the loop. This means that the loop will not continue to search for additional multiples of 7, even though they may exist later in the list.

Continue Statement

The continue statement is used to skip the current iteration of a loop and move on to the next iteration. Unlike the break statement, the continue statement does not exit the loop. Instead, it simply skips the rest of the statements in the current iteration and continues to the next iteration.

For example, consider the following code that uses a for loop to print only the odd numbers in a list:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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

The output of the above code will be:

1
3
5
7
9

In this example, the loop iterates through the list of numbers and checks each number to see if it is even. If a number is even, the continue statement is executed and the loop skips the rest of the statements for that iteration and moves on to the next iteration. If a number is odd, the loop prints the number and then continues to the next iteration.

It is important to note that the continue statement can only be used inside a loop. If you try to use it outside of a loop, you will get a syntax error.

Using Break and Continue Together

It is possible to use both the break and continue statements in the same loop. In such cases, the order in which they are used can have a significant impact on the behavior of the loop. For example:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in numbers:
    if number % 2 == 0:
        print("Even number found:", number)
        break
    else:
        continue
    print("This statement will not be executed")

In this example, the loop iterates through the list of numbers and checks each number to see if it is even. If a number is even, the loop prints the message “Even number found” and then uses a break statement to exit the loop. If a number is odd, the continue statement is executed and the loop skips the rest of the statements for that iteration and moves on to the next iteration. As a result, the print statement after the continue statement will not be executed for any iteration.

In conclusion, the break and continue statements are powerful tools for controlling the flow of execution in loops. They allow you to skip certain iterations or exit the loop prematurely, giving you more control over how your program runs. However, it is important to use them carefully and understand their behavior to avoid unexpected results.

Leave a Reply

Your email address will not be published. Required fields are marked *