Break, Continue & Pass
Python provides break, continue and pass statements to control the execution of loops.
Break, Continue & Pass
At CIIT Training Institute, loop control statements are explained using practical examples so students can understand exactly when a loop should stop, skip an iteration or simply do nothing.
The three important statements are: break → stop the loop, continue → skip the current iteration, pass → do nothing and continue normally.
What are Loop Control Statements?
Normally, a loop executes its statements one after another. Loop control statements allow us to change this normal execution flow.
| Statement | Purpose | Simple Meaning |
|---|---|---|
break
|
Terminates the loop completely. | Stop the loop. |
continue
|
Skips the current iteration. | Skip this turn. |
pass
|
Performs no operation. | Do nothing for now. |
Loop Control Flow
↓
Exit Loop
↓
Next Iteration
↓
Do Nothing
1. break Statement
The break statement is used to
immediately terminate a loop.
Once Python executes break, the loop
stops completely and program execution continues
with the statement after the loop.
Basic Example
for number in range(1, 10):
if number == 5:
break
print(number)
Output
1
2
3
4
When number becomes 5, the
break statement terminates the loop.
How break Works
for number in range(1, 6):
print("Checking:", number)
if number == 3:
break
print("Loop ended")
Output
Checking: 1
Checking: 2
Checking: 3
Loop ended
The loop does not continue with 4 and 5 because
break has already stopped it.
break With While Loop
number = 1
while number <= 10:
if number == 6:
break
print(number)
number += 1
Output
1
2
3
4
5
Example: Search Student 🤓🔓
Suppose we have a list of students and want to stop searching as soon as we find a particular student.
students = [
"Rahul",
"Priya",
"Sam",
"Amit",
"Sneha"
]
for student in students:
print("Checking:", student)
if student == "Sam":
print("Student Found!")
break
Output
Checking: Rahul
Checking: Priya
Checking: Sam
Student Found!
Once Sam is found, there is no need to check the remaining students.
2. continue Statement
The continue statement skips 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
Number 3 is skipped, but the loop continues with numbers 4 and 5.
How continue Works
for number in range(1, 6):
if number == 3:
continue
print("Number:", number)
Output
Number: 1
Number: 2
Number: 4
Number: 5
When the value is 3, Python skips the remaining statements of that iteration.
Example: Skip Odd Numbers 📩🔓
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for number in numbers:
if number % 2 != 0:
continue
print(number)
Output
2
4
6
8
Odd numbers are skipped using
continue, so only even numbers
are printed.
Example: Skip Inactive Students 🤓🔓
students = [
("Sam", True),
("Rahul", False),
("Priya", True),
("Amit", False)
]
for name, active in students:
if not active:
continue
print("Active Student:", name)
Output
Active Student: Sam
Active Student: Priya
3. pass Statement
The pass statement is a placeholder.
It does nothing when executed.
It is useful when we are planning to add code later but Python currently requires a valid statement.
Basic Example
for number in range(1, 6):
if number == 3:
pass
print(number)
Output
1
2
3
4
5
Unlike break and
continue, pass does not
change the loop flow.
pass as a Placeholder
pass is commonly used while creating
an incomplete function, class or conditional block.
def calculate_result():
pass
The function can be completed later without causing an indentation or syntax problem.
Another Example
class Student:
pass
break vs continue vs pass
| Feature | break | continue | pass |
|---|---|---|---|
| Stops loop | Yes | No | No |
| Skips current iteration | No | Yes | No |
| Does nothing | No | No | Yes |
| Main purpose | Exit loop | Skip iteration | Placeholder |
Using break and continue Together
Both statements can be used in the same loop when different conditions require different actions.
for number in range(1, 11):
if number == 3:
continue
if number == 8:
break
print(number)
Output
1
2
4
5
6
7
Number 3 is skipped, while the loop completely stops when number reaches 8.
break in Nested Loops
When break is used inside a nested loop,
it terminates the nearest loop
in which it appears.
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
break
print(i, j)
Output
1 1
2 1
3 1
continue in Nested Loops
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
continue
print(i, j)
Output
1 1
1 3
2 1
2 3
3 1
3 3
The value 2 is skipped in the inner loop while the outer loop continues normally.
Example: Course Search 📩🤓
courses = [
"Python",
".NET",
"Java",
"Data Science"
]
search = "Java"
for course in courses:
if course != search:
continue
print("Course Found:", course)
break
Output
Course Found: Java
Common Mistakes
| Mistake | Explanation |
|---|---|
| Using break when you only want to skip |
Use continue when the loop
should continue.
|
| Using continue without understanding loop flow | The remaining statements in the current iteration are skipped. |
| Expecting pass to skip an iteration |
pass does not skip anything.
It simply does nothing.
|
| Forgetting indentation | Python depends on indentation to define code blocks. |
| Infinite loop with continue | In a while loop, make sure the required variable update still happens. |
Important Interview Questions
-
What is the purpose of the
breakstatement? -
What is the difference between
breakandcontinue? -
What does the
passstatement do? -
Does
continueterminate a loop? -
What happens when
breakis used inside a nested loop? -
Why is
passuseful when writing incomplete code?
CIIT Learning Point
Remember:
Use break when you want to
stop the loop, use
continue when you want to
skip the current iteration, and use
pass when you need a
placeholder without performing any
operation. Understanding the difference between these
three statements is important for both real-world
Python programming and coding interviews.
CIIT Practice Tasks
-
Print numbers from 1 to 10, but stop the loop
when the number becomes 6 using
break. -
Print numbers from 1 to 20 but skip all
multiples of 3 using
continue. - Create a student list and search for a particular student. Stop the search when the student is found.
-
Create a list of numbers and print only even
numbers using
continue. -
Create a function with
passas a temporary placeholder. -
Create a nested loop and use both
breakandcontinueto control its execution.
Summary :
Python provides three important loop control statements:
break, continue and
pass. The break statement
completely terminates the loop, while
continue skips the current iteration and
moves to the next one. The pass statement
performs no operation and is mainly used as a
placeholder. These statements give developers better
control over loop execution and are commonly used in
practical Python programs.