Example for Break and Continue in Python

An example of the use of break and continue statements in Python:

# Example 1: break statement
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers:
    if number == 6:
        break
    print(number)

# Output:
# 1
# 2
# 3
# 4
# 5

# Example 2: continue statement
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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

# Output:
# 1
# 3
# 5
# 7
# 9
Code language: Python (python)

In the first example, we use the break statement to exit the loop when the number becomes equal to 6. As a result, only the numbers 1 to 5 are printed.

In the second example, we use the continue statement to skip the even numbers.

When the condition number % 2 == 0 is satisfied, the continue statement is encountered, and the loop jumps to the next iteration.

As a result, only the odd numbers (1, 3, 5, 7, 9) are printed.

What is break and continue in Python explain with an example?

break and continue are control flow statements in Python that alter the behavior of loops.

Break:

The break statement is used to exit or terminate a loop prematurely.

When encountered within a loop, it immediately terminates the loop’s execution, regardless of whether the loop’s condition has been satisfied or not.

It allows you to “break out” of the loop and continue with the next section of code after the loop.

Here’s an example that uses break:

# Example 1: break statement
numbers = [1, 2, 3, 4, 5]

for number in numbers:
    if number == 3:
        break
    print(number)

# Output:
# 1
# 2
Code language: Python (python)

In this example, the loop iterates over the numbers list.

When the value of number becomes equal to 3, the break statement is encountered, causing the loop to terminate immediately.

As a result, only the numbers 1 and 2 are printed.

Continue:

The continue statement is used to skip the current iteration of a loop and move to the next iteration.

When encountered within a loop, it jumps to the next iteration without executing any further statements in the loop’s body.

Here’s an example that uses continue:

# Example 2: continue statement
numbers = [1, 2, 3, 4, 5]

for number in numbers:
    if number == 3:
        continue
    print(number)

# Output:
# 1
# 2
# 4
# 5
Code language: Python (python)

In this example, when the value of number becomes equal to 3, the continue statement is encountered.

As a result, the current iteration is skipped, and the loop proceeds to the next iteration.

Thus, the number 3 is not printed, and the loop continues with the remaining numbers.

To summarize, break is used to terminate the loop entirely, while continue is used to skip the current iteration and move on to the next one.

Read More;

  • Dmytro Iliushko

    I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

    View all posts

Leave a Comment