Python

 

Introduction

In Python, control flow statements such as pass, break, and continue allow developers to alter the flow of execution in loops and conditional blocks. These statements play a crucial role in decision-making and controlling how loops behave. This guide aims to explain the function and use of each of these statements with examples and practical usage.

Objective

The objective of this topic is to understand the functionality and appropriate use cases of Python’s pass, break, and continue statements. By the end of this tutorial, you’ll be able to efficiently use these statements in your Python programs to control loop behavior and manage flow more effectively.

Code Example


# Example demonstrating the use of pass, break, and continue statements in Python

# Using 'pass' to do nothing in a loop
for i in range(5):
    if i == 2:
        pass  # Do nothing when i is 2
    else:
        print(f"Pass statement is skipping 2: {i}")

# Using 'break' to stop a loop early
for i in range(5):
    if i == 3:
        print("Breaking the loop at i = 3")
        break  # Exit the loop when i reaches 3
    print(f"Breaking the loop: {i}")

# Using 'continue' to skip the current iteration of a loop
for i in range(5):
    if i == 2:
        print("Skipping 2 with continue")
        continue  # Skip printing 2
    print(f"Continue statement skipped 2: {i}")

Program Explanation

This program demonstrates the use of three important Python control flow statements: pass, break, and continue.

  • pass: The pass statement is used when you want to write a placeholder for future code. It allows the program to continue without doing anything. In the first loop, when i == 2, the program does nothing and skips the iteration.
  • break: The break statement immediately stops the loop when a condition is met. In the second loop, the loop breaks when i == 3, stopping further iterations.
  • continue: The continue statement skips the current iteration and proceeds to the next iteration. In the third loop, when i == 2, the loop skips printing 2 and continues with the next iteration.

How to Run the Program

To run the program, follow these steps:

  1. Open a Python IDE or a text editor of your choice.
  2. Copy and paste the code provided above into your editor.
  3. Save the file with a .py extension (e.g., control_statements.py).
  4. Run the program in your terminal or IDE.

You should see the output demonstrating how each of the statements works in the loop.

© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)