Python break, continue, pass: Loop Control/Jumping Statements

Loop Control Statements in Python

In addition to common control statements like if-else, while loops, and for loops, Python includes special loop control statements that offer more control over program execution. These are:

  1. break
  2. continue
  3. pass

Each of these statements has unique features that allow you to control the flow of your program in ways that standard control statements may not.

1. Python break

The break statement is used to exit a loop prematurely, stopping the loop’s execution entirely. This can be useful when you need to terminate the loop based on a specific condition.

Example 1: Breaking a For Loop

				for i in range(0, 10):
    if i == 5:
        break
    print("Range Value:", i)

			

In this example, the loop stops running when i equals 5, and no further values are printed.

python break loop control statement

Example 2: Breaking a Loop in a String

				for char in 'Python':
    if char == 't':
        break
    print("Current Letter:", char)

			

Here, the loop exits when it encounters the letter ‘t‘, so only the letters before ‘t’ are printed.

Breaking a Loop in a String - Python break statement

These examples illustrate how the break statement allows you to exit loops early based on specific conditions, enhancing your control over the flow of your program.

2. CONTINUE

The continue statement skips the rest of the code in the current iteration and moves to the next iteration of the loop.

Example 1: Using Continue in a For Loop

				for i in range(0, 10):
    if i == 5:
        continue
    print("Range Value:", i)

			
Python continue loop control statement

Example 2: Using Continue with a String

				for char in 'Python':
    if char == 't':
        continue
    print("Current Letter:", char)

			
Skiping a Loop in a String - Python continue statement

3. PASS

The pass statement is a placeholder that does nothing when executed. It is useful during development when you want to leave a block of code empty but still need to maintain the structure of your program.

Example 1: Using Pass in a For Loop

				for i in range(0, 10):
    if i == 5:
        pass
        print("pass block for range value(5)")
    print("Range Value:", i)

print("Good bye - Example 1")

			
Python pass loop control statement

Example 2: Using Pass with a String

				for char in 'Python':
    if char == 't':
        pass
        print("pass block for character(t)")
    print("Current Letter:", char)

print("Good bye - Example 2")

			
Skiping a Loop in a String - Python pass statement

Summary

Python offers special loop control statements—python break, continue, and pass—to manage the flow of loops more effectively:

  • Break: Exits a loop completely when a specific condition is met.
  • Continue: Skips the current iteration and moves to the next one.
  • Pass: Acts as a placeholder, doing nothing when executed, useful during development.

These statements give you greater flexibility in controlling program execution within loops.