DEV Community

Techno-101
Techno-101

Posted on

Break, Pass, and Continue Statements in Python

In Python, break, pass, and continue are control flow statements used within loops (like for and while) to alter the flow of execution.

break statement:

The break statement is used to exit a loop prematurely. When the break statement is encountered inside a loop, the loop is terminated immediately, and the program control resumes at the next statement after the loop.
Example:

for i in range(5):
    if i == 3:
        break
    print(i)

Enter fullscreen mode Exit fullscreen mode

Output:
0
1
2
pass statement:

The pass statement is a null operation; nothing happens when it is executed. It is used as a placeholder when a statement is syntactically required but you do not want any action to occur.
Example:

for i in range(5):
    if i == 3:
        pass
    else:
        print(i)

Enter fullscreen mode Exit fullscreen mode

Output:
0
1
2
4
continue statement:

The continue statement is used to skip the rest of the code inside a loop for the current iteration and continue with the next iteration.
Example:

for i in range(5):
    if i == 3:
        continue
    print(i)

Enter fullscreen mode Exit fullscreen mode

Output:
0
1
2
4
These statements provide flexibility and control over loops in Python, allowing you to tailor the loop's behavior to specific requirements.

For more to learn Python, check out the data science course and data science tutorial!

Top comments (0)