Python break and continue: Explained with Examples

Introduction: Python break and continue

The break and continue statements are control flow statements in Python that allow you to modify the behaviour of loops. They provide additional control and flexibility in deciding when to terminate a loop or skip certain iterations. In this article, we will explore the concepts of break and continue in Python, understand their differences, and provide examples to illustrate their usage.

The break Statement:

The break statement is used to exit a loop prematurely, even if the loop condition is still true. When the break statement is encountered inside a loop, the loop is immediately terminated, and the program execution continues to the next statement outside the loop. It is commonly used to stop the execution of a loop when a specific condition is met.

Example 1: Using break in a while loop

count = 1
while True:
    print(count)
    count += 1
    if count > 5:
        break

Output:

1
2
3
4
5

In this example, the while loop continues indefinitely. However, when the value of “count” becomes greater than 5, the break statement is executed, causing the loop to terminate. As a result, the numbers 1 to 5 are printed, and the program execution moves on.

Example 2: Using break in a for loop

fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
    if fruit == 'banana':
        break
    print(fruit)

Output:

apple

In this example, the for loop iterates over a list of fruits. When the loop encounters the string ‘banana’, the break statement is executed, and the loop is abruptly terminated. As a result, only the first fruit, ‘apple’, is printed.

See Also  Python Keywords and Identifiers

The continue Statement: The continue statement is used to skip the rest of the code block for the current iteration of a loop and move on to the next iteration. It allows you to control which iterations should be executed and which should be skipped.

Example 1: Using continue in a while loop

count = 1
while count <= 5:
    if count == 3:
        count += 1
        continue
    print(count)
    count += 1

Output:

1
2
4
5

In this example, the while loop iterates from 1 to 5. However, when the value of “count” is equal to 3, the continue statement is executed. This causes the rest of the code block to be skipped for that iteration, and the loop proceeds to the next iteration. As a result, the number 3 is not printed.

Example 2: Using continue in a for loop

fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
    if fruit == 'banana':
        continue
    print(fruit)

Output:

apple
orange

In this example, the for loop iterates over a list of fruits. When the loop encounters the string ‘banana’, the continue statement is executed. This skips the remaining code block for that iteration and moves on to the next iteration. As a result, ‘banana’ is not printed.

Key Differences between break and continue:

  • The break statement terminates the entire loop and proceeds to the next statement outside the loop. The continue statement skips the remaining code for the current iteration and moves to the next iteration of the loop.
  • Break is used to exit the loop entirely, while continue is used to skip a specific iteration.
  • Break can be used in both while and for loops, whereas continue is only applicable in loop structures.
See Also  Python @property Decorator: Simplifying Property Management

The break and continue statements are powerful control flow tools in Python that allow you to modify the behavior of loops. The break statement is used to exit a loop prematurely, while the continue statement skips the remaining code for the current iteration. Understanding when and how to use break and continue will help you write more efficient and concise code, enabling you to have greater control over the flow of your program.

5 1 vote
Article Rating

Related articles

Python Regular Expressions

Python Regular Expressions: Effortlessly match, search, and manipulate text patterns with precision and flexibility. Boost your text processing capabilities now

Python @property Decorator: Simplifying Property Management

Python's @property decorator simplifies property management. It transforms methods into attributes, providing controlled access to data with clean syntax.

Python Decorators: Enhancing Functionality with Elegance

Python decorators: Enhance function and class behavior dynamically. Add functionalities like logging, caching, and authentication with elegance and simplicity.

Python Closures: Mastering Function Encapsulation

Encapsulate state, create specialized functions and implement advanced patterns in just a few lines of code. Unleash the flexibility of Python Closures

Python Generators: Efficient Approach to Iteration

Python generators provide a memory-efficient and concise way to create iterators. They generate values on the fly, improving performance when working with large

Case Studies

Compass Music Platform

A clothing brand wanted to launch a new e-commerce website that would allow customers to browse and purchase their products online. We developed a...

NewsWeek Magazine

A clothing brand wanted to launch a new e-commerce website that would allow customers to browse and purchase their products online. We developed a...

Beauty & Makeup Shop

A clothing brand wanted to launch a new e-commerce website that would allow customers to browse and purchase their products online. We developed a...
0
Would love your thoughts, please comment.x
()
x