Home Course Roadmap
Lesson 27 · Control Flow Beginner

break, continue & pass

Learn how to control the execution of Python loops using break, continue, and pass. These statements give you precise control over what happens inside loops and conditional blocks.

35–45 minutes
Control Flow
Python Foundations
0%

What You Will Learn

  • Understand how loop control statements work.
  • Use break to terminate loops.
  • Use continue to skip iterations.
  • Understand the purpose of pass.
  • Control nested loops carefully.
  • Use loop control statements in real programs.
  • Understand how break interacts with loop else.
  • Avoid common control-flow mistakes.
01

Controlling Loops

In the previous lessons, you learned how for and while loops repeat code.

Sometimes, however, simply repeating the same block of code is not enough. A program may need to:

  • Stop a loop immediately.
  • Skip a particular iteration.
  • Leave a block temporarily empty.

Python provides three useful statements for these situations:

Three control statements
break
continue
pass

Simple Mental Model

  • break → Stop the loop.
  • continue → Skip this iteration.
  • pass → Do nothing.
02

The break Statement

The break statement immediately terminates the nearest enclosing loop.

Basic example
for number in range(1, 11):

    if number == 5:
        break

    print(number)
Output
1
2
3
4

When number becomes 5, Python encounters break and immediately exits the loop.

Key Idea

break does not pause the loop. It completely terminates the current loop.

03

break with for

A for loop normally continues until all items have been processed. break lets you stop early when a particular condition is reached.

Stop at a specific value
for number in [10, 20, 30, 40, 50]:

    if number == 30:
        break

    print(number)
Output
10
20

The values after 30 are never processed because the loop ends when 30 is encountered.

Stop When a Condition Is Met

Example
names = ["Ali", "Sara", "Ahmed", "Fatima"]

for name in names:

    if name == "Ahmed":
        break

    print(name)
Output
Ali
Sara
04

break with while

break is particularly useful in while loops when you want to create a loop that runs until an explicit exit condition occurs.

Example
number = 1

while True:

    print(number)

    if number == 5:
        break

    number += 1
Output
1
2
3
4
5

Here, while True creates a loop that would otherwise continue forever. The break statement provides the exit mechanism.

Common Pattern

The combination of while True and break is often used for menu-driven programs, command processors and repeated user input.

05

Searching with break

One of the most practical uses of break is stopping a search as soon as the desired item is found.

Search example
products = ["Laptop", "Mouse", "Keyboard", "Monitor"]

search = "Keyboard"

for product in products:

    if product == search:
        print("Product found!")
        break
Output
Product found!

Once the product is found, continuing through the rest of the list would be unnecessary.

Search with a Result Variable

More complete example
numbers = [12, 18, 25, 31, 44]

target = 31
found = False

for number in numbers:

    if number == target:
        found = True
        break

print("Found:", found)
Output
Found: True
06

The continue Statement

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

Basic example
for number in range(1, 6):

    if number == 3:
        continue

    print(number)
Output
1
2
4
5

When the number is 3, Python skips the print() statement and continues with the next iteration.

Key Idea

continue does not terminate the loop. It only skips the current iteration.

07

continue with for

continue is useful when you want to ignore certain items but continue processing the rest.

Print Only Odd Numbers

Example
for number in range(1, 11):

    if number % 2 == 0:
        continue

    print(number)
Output
1
3
5
7
9

Skip Invalid Values

Data filtering
values = [10, -5, 20, -2, 30]

for value in values:

    if value < 0:
        continue

    print(value)
Output
10
20
30
08

continue with while

continue can also be used inside a while loop, but you must carefully manage the loop-control variable.

Correct example
number = 0

while number < 6:

    number += 1

    if number == 3:
        continue

    print(number)
Output
1
2
4
5
6

Notice that number += 1 happens before continue. This ensures that the loop continues making progress.

Important Warning

If a continue statement skips the code responsible for updating your loop variable, the loop can become infinite.

09

The pass Statement

The pass statement does nothing.

It is used when Python requires a statement but you do not want the program to perform an action yet.

Basic example
for number in range(5):

    if number == 3:
        pass

    print(number)
Output
0
1
2
3
4

Unlike continue, pass does not skip the rest of the loop iteration. The next statement still executes.

Important Difference

pass is simply a placeholder. It does not change the normal flow of execution.

10

When to Use pass

The most common purpose of pass is to create a syntactically valid placeholder while you plan the implementation.

Placeholder Function

Example
def calculate_salary():
    pass

Without pass, the function would have no valid body.

Placeholder Class

Example
class Customer:
    pass

This creates a valid empty class that can be developed later.

Placeholder Conditional Block

Example
age = 25

if age >= 18:
    pass
else:
    print("Underage")

The if block intentionally performs no action.

Don't Confuse pass with continue

pass means "do nothing here." continue means "skip the rest of this iteration and move to the next one."

11

break vs continue vs pass

Understanding the difference between these three statements is extremely important.

Comparison
break     → Exit the loop completely

continue  → Skip the current iteration

pass      → Do nothing

Visualizing the Difference

break
for number in range(5):

    if number == 2:
        break

    print(number)
Output
0
1
continue
for number in range(5):

    if number == 2:
        continue

    print(number)
Output
0
1
3
4
pass
for number in range(5):

    if number == 2:
        pass

    print(number)
Output
0
1
2
3
4
12

Control Statements in Nested Loops

When loops are nested, break and continue affect the nearest enclosing loop.

Nested loop example
for outer in range(3):

    for inner in range(3):

        if inner == 1:
            break

        print(outer, inner)
Output
0 0
1 0
2 0

The break statement terminates only the inner loop. The outer loop continues normally.

Important Rule

A break statement does not automatically terminate every nested loop. It exits only the nearest enclosing loop.

13

Loop else and break

Python's loop else block is especially useful when combined with break.

The else block executes when the loop finishes normally. It does not execute if the loop is terminated by break.

Search example
numbers = [10, 20, 30, 40]

target = 25

for number in numbers:

    if number == target:
        print("Found")
        break

else:
    print("Not found")
Output
Not found

Because 25 was never found, the loop completed normally and the else block executed.

Useful Pattern

This pattern is excellent for search operations: break when the item is found and else when the search finishes without finding it.

14

Practical Real-World Examples

Example 1 — Stop at a Failed Transaction

Transaction processing
transactions = [1000, 2500, -1, 3000, 4500]

for amount in transactions:

    if amount < 0:
        print("Invalid transaction found.")
        break

    print("Processing:", amount)
Output
Processing: 1000
Processing: 2500
Invalid transaction found.

Example 2 — Skip Invalid Marks

Student data
marks = [78, -1, 91, 65, -5, 88]

for mark in marks:

    if mark < 0:
        continue

    print("Valid mark:", mark)
Output
Valid mark: 78
Valid mark: 91
Valid mark: 65
Valid mark: 88

Example 3 — Login Attempts

Authentication example
correct_password = "python123"

for attempt in range(3):

    password = input("Enter password: ")

    if password == correct_password:
        print("Login successful.")
        break

    print("Incorrect password.")

else:
    print("Account locked.")

Example 4 — Processing Orders

Order processing
orders = [
    "Order #101",
    "Order #102",
    "INVALID",
    "Order #104"
]

for order in orders:

    if order == "INVALID":
        print("Invalid order detected.")
        continue

    print("Processing:", order)
Output
Processing: Order #101
Processing: Order #102
Processing: Order #104

Example 5 — Placeholder for Future Logic

Using pass
for feature in ["login", "payments", "reports"]:

    if feature == "payments":
        pass

    print("Feature:", feature)

The pass statement allows the block to remain empty while keeping the program syntactically valid.

15

Common Mistakes

1. Thinking continue Ends the Loop

This is incorrect. continue only skips the current iteration.

Remember

Use break when you want to exit the loop. Use continue when you want to skip one iteration.

2. Thinking pass Skips an Iteration

pass does not skip anything. It simply performs no operation.

3. Creating an Infinite Loop with continue

Problematic pattern
number = 0

while number < 5:

    if number == 2:
        continue

    number += 1

When number becomes 2, the program reaches continue before increasing the number. The condition remains true forever.

4. Expecting break to Exit All Nested Loops

A single break only exits the nearest enclosing loop.

5. Using pass When Real Logic Is Required

pass is useful as a placeholder, but it should not be used to hide unfinished logic in a completed program.

16

Practice Exercises

Exercise 1 — Stop at 5

Use a for loop and break to print numbers from 1 to 10 but stop when the number reaches 5.

Exercise 2 — Skip Even Numbers

Use continue to print only odd numbers between 1 and 20.

Exercise 3 — Search a List

Search a list of names. Stop searching when a particular name is found.

Exercise 4 — Invalid Data

Given a list of numbers, skip negative values and print only valid positive values.

Exercise 5 — Password Attempts

Give the user three chances to enter the correct password. Use break after successful login.

Exercise 6 — Use pass

Create an empty function and use pass as its placeholder body.

Exercise 7 — Search with for...else

Search for a number in a list and display "Found" or "Not found" using break and else.

Exercise 8 — Nested Loops

Create nested loops and use break to stop only the inner loop.

17

Mini Challenge — Smart Number Search

Build a program that searches a list of numbers and intelligently handles the data.

Your program should:

  1. Store a list of numbers.
  2. Ask the user for a number to search for.
  3. Ignore negative values using continue.
  4. Stop searching when the target is found using break.
  5. Use for...else to display a message if the target is not found.
  6. Count how many valid numbers were checked.
Starter Code
numbers = [10, -5, 20, 35, -2, 40, 55]

target = int(input("Enter number to search: "))

checked = 0

for number in numbers:

    if number < 0:
        continue

    checked += 1

    if number == target:
        print("Number found!")
        break

else:
    print("Number not found.")

print("Valid numbers checked:", checked)

Upgrade the Challenge

Modify the program so that it reports the position of the target number when it is found.

Challenge Goal

The goal is not just to make the program work. Understand exactly why each of continue, break and else is being used.

18

Quick Knowledge Check

1. What does break do?

2. What does continue do?

3. What does pass do?

4. Which statement should you use to stop a loop?

5. What happens when continue executes?

6. What does a single break do inside nested loops?

7. When does a loop's else block normally execute?

8. Which statement is commonly used as a placeholder?

19

Lesson Summary

Python provides three important statements for controlling program flow: break, continue, and pass.

  • break immediately exits the nearest loop.
  • continue skips the current iteration.
  • pass performs no operation.
  • break is useful for searches and early exits.
  • continue is useful for filtering or skipping unwanted data.
  • pass is useful as a placeholder for future code.
  • In nested loops, break affects only the nearest loop.
  • Loop else runs when a loop completes normally without break.
  • Careless use of continue inside while loops can create infinite loops.
Control Flow Is Getting Powerful

You can now decide not only how loops repeat, but also exactly when iterations should be skipped, stopped, or intentionally left empty.

Ready for the Next Lesson?

Complete the exercises and Smart Number Search challenge, then mark this lesson as complete.