Break & Continue

Break & Continue

·

4 min read

What is Break Statement?

In Python, the break statement is a control flow statement that allows you to exit a loop prematurely. When encountered within a loop, the break statement immediately terminates the loop's execution and transfers the program's control to the next statement after the loop.

Here's the general syntax of the break statement:

while condition:
    # code block

    if condition:
        break

    # code continues outside the loop

In this syntax:

  • The while loop is used as an example, but the break statement can also be used within for loops.

  • The loop will continue executing as long as the condition is true.

  • Within the loop, you can include an if statement that checks a specific condition.

  • If the condition within the if statement evaluates to True, the break statement is encountered, and the loop is immediately terminated.

  • The program's control is then transferred to the next statement outside the loop.

Here are a couple of examples to illustrate the usage of the break statement:

Examples:

Example 1: Breaking out of a while loop based on a condition

count = 0

while True:
    print(count)
    count += 1

    if count == 5:
        break

print("Loop ended")

Output:

0
1
2
3
4
Loop ended

In this example, the while loop continues indefinitely (while True), but the break statement is encountered when the count reaches 5. As a result, the loop is terminated, and the message "Loop ended" is printed.

Example 2: Breaking out of a for loop based on a condition

fruits = ["apple", "banana", "cherry", "date", "elderberry"]

for fruit in fruits:
    print(fruit)

    if fruit == "cherry":
        break

Output:

apple
banana
cherry

In this example, the for loop iterates over the list of the fruits. When the loop encounters the "cherry" element, the break statement is executed, causing the loop to terminate. As a result, only the elements "apple," "banana," and "cherry" are printed.

The break statement is useful when you want to exit a loop prematurely based on a specific condition. It allows you to control the flow of your program and skip the remaining iterations of a loop when necessary.

What is Continue Statement?

In Python, the continue statement is a control flow statement that allows you to skip the rest of the current iteration of a loop and move to the next iteration. When encountered within a loop, the continue statement immediately jumps to the next iteration, skipping any remaining statements in the loop's body.

Here's the general syntax of the continue statement:

while condition:
    # code block

    if condition:
        continue

    # code continues within the loop if condition is not met

In this syntax:

  • The while loop is used as an example, but the continue statement can also be used within for loops.

  • The loop will continue executing as long as the condition is true.

  • Within the loop, you can include an if statement that checks a specific condition.

  • If the condition within the if statement evaluates to True, the continue statement is encountered, and the rest of the current iteration is skipped.

  • The program's control is then transferred to the next iteration of the loop.

Here are a couple of examples to illustrate the usage of the continue statement:

Examples:

Example 1: Skipping the rest of the current iteration in a while loop

count = 0

while count < 5:
    count += 1

    if count == 3:
        continue

    print(count)

print("Loop ended")

Output:

1
2
4
5
Loop ended

In this example, the while loop iterates over the numbers from 1 to 5. When the loop encounters the value 3, the continue statement is executed, skipping the print statement for that iteration. As a result, the number 3 is not printed, and the loop moves to the next iteration.

Example 2: Skipping specific elements in a for loop

fruits = ["apple", "banana", "cherry", "date", "elderberry"]

for fruit in fruits:
    if fruit == "cherry":
        continue

    print(fruit)

print("Loop ended")

Output:

apple
banana
date
elderberry
Loop ended

In this example, the for loop iterates over the list of the fruits. When the loop encounters the element "cherry", the continue statement is executed, skipping the print statement for that iteration. As a result, the element "cherry" is not printed, and the loop moves to the next iteration.

The continue statement is useful when you want to skip certain parts of a loop's iteration based on a specific condition. It allows you to control the flow of your program within a loop and selectively skip over certain iterations when necessary.

Conclusion.

In conclusion, the break statement and continue statement in Python provides powerful control flow options within loops. The break statement allows for the termination of a loop prematurely when a specific condition is met, while the continue statement skips the remaining code in the current iteration and proceeds to the next iteration. These statements enable programmers to have more control over loop execution and improve code efficiency by selectively ending or skipping iterations based on conditions. By leveraging the capabilities of break and continue, developers can write more flexible and concise code for handling repetitive tasks.