Skip to Content
Course content

2.4.3 Break, Continue, and Pass in Python

In Python, break, continue, and pass are control flow statements that are used to manage the behavior of loops and conditional statements. Here's a detailed explanation of each:

1. break: Exits the loop early

The break statement is used to terminate a loop prematurely, regardless of whether the loop's condition has been fully evaluated or not. When Python encounters a break statement inside a loop, it immediately stops executing the loop and continues with the next statement after the loop.

Syntax:

for item in sequence:
    if condition:
        break  # Exit the loop early
    # rest of the loop body

Example:

for i in range(1, 6):
    if i == 3:
        break  # Exit the loop when i equals 3
    print(i)
  • Output:
    1
    2
    
  • Explanation:
    • The loop starts with i = 1 and prints 1.
    • Then it goes to i = 2, prints 2, and continues.
    • When i becomes 3, the break statement is triggered, and the loop terminates. As a result, 3, 4, and 5 are never printed.

2. continue: Skips to the next iteration

The continue statement is used to skip the current iteration of the loop and move on to the next iteration. When Python encounters a continue statement, it ignores the remaining code in the current loop iteration and jumps to the next one.

Syntax:

for item in sequence:
    if condition:
        continue  # Skip this iteration and continue with the next
    # rest of the loop body

Example:

for i in range(1, 6):
    if i == 3:
        continue  # Skip this iteration when i equals 3
    print(i)
  • Output:
    1
    2
    4
    5
    
  • Explanation:
    • The loop starts by printing numbers from 1 to 5.
    • When i = 3, the continue statement is triggered, causing the loop to skip the print(i) statement for that iteration. So, 3 is not printed.
    • The loop continues with the next iteration (when i = 4).

3. pass: Placeholder for a statement that does nothing

The pass statement is used as a placeholder where a statement is syntactically required but no action is needed. It's a "do nothing" operation. It is useful when you want to define an empty function, class, or loop without causing an error.

Syntax:

if condition:
    pass  # Does nothing, just a placeholder

Example 1: Using pass in a loop

for i in range(1, 6):
    if i == 3:
        pass  # Do nothing when i is 3
    else:
        print(i)
  • Output:
    1
    2
    4
    5
    
  • Explanation:
    • The loop iterates from 1 to 5, printing all values except when i is 3.
    • When i is 3, the pass statement is encountered, and no action is performed for that iteration. The loop continues as usual for the remaining numbers.

Example 2: Using pass in a function or class

def placeholder_function():
    pass  # Placeholder for code that will be written later

class EmptyClass:
    pass  # Empty class definition
  • Explanation:
    • The pass statement allows you to define a function or class without having to implement its functionality immediately.
    • This is useful during the development phase when you might want to structure your code but don't yet have the logic for certain parts.

Summary of Control Statements:

  1. break:
    • Used to exit the loop early.
    • Halts further iterations of the loop.
  2. continue:
    • Skips the rest of the current iteration and proceeds with the next iteration.
  3. pass:
    • A placeholder that does nothing.
    • Used where Python requires a statement but no action is needed.

Each of these control statements plays an important role in managing the flow of loops and conditionals, helping you write more efficient and readable code.

Commenting is not enabled on this course.