1. Introduction
In Python, break, continue and
pass are special statements used to control the flow
of a program.
These statements are especially important when working with
for and while loops.
break
Stops the loop completely.
continue
Skips the current iteration.
pass
Does nothing.
2. break Statement
The break statement is used to terminate a loop
immediately.
break statement terminates the nearest enclosing for or while loop.
Syntax
break
Example
for i in range(1, 11):
if i == 6:
break
print(i)
1
2
3
4
5
When i becomes 6, the
break statement is executed and the loop terminates.
3. How break Works
4. Practical Example of break
Suppose we want to search for a particular number in a list. Once the number is found, we can stop searching.
Example
numbers = [10, 20, 30, 40, 50]
for number in numbers:
if number == 30:
print("Number found")
break
Number found
5. continue Statement
The continue statement skips the remaining statements
of the current iteration and moves to the next iteration.
continue does not terminate the loop.
It only skips the current iteration.
Syntax
continue
Example
for i in range(1, 11):
if i == 6:
continue
print(i)
1
2
3
4
5
7
8
9
10
The value 6 is skipped, but the loop continues with
the next iteration.
6. How continue Works
7. Practical Example of continue
The following program uses continue to skip even numbers.
Example
for number in range(1, 11):
if number % 2 == 0:
continue
print(number)
1
3
5
7
9
8. pass Statement
The pass statement is a null statement.
It does not perform any operation.
pass is used as a placeholder when a programmer
needs a statement syntactically but does not want to execute
any action.
Syntax
pass
Example
for i in range(1, 6):
if i == 3:
pass
print(i)
1
2
3
4
5
When i = 3, pass does nothing.
Therefore, 3 is still printed.
9. Practical Example of pass
The pass statement is useful when a programmer wants
to create a function but will implement it later.
Example
def calculate_result():
pass
print("Program completed")
Program completed
10. Difference Between break, continue and pass
| Statement | Purpose | Effect | Loop Continues? |
|---|---|---|---|
break |
Stop the loop | Terminates the loop | No |
continue |
Skip iteration | Moves to next iteration | Yes |
pass |
Do nothing | No operation | Yes |
11. Comparing All Three Statements
Using break
for i in range(1, 6):
if i == 3:
break
print(i)
1
2
Using continue
for i in range(1, 6):
if i == 3:
continue
print(i)
1
2
4
5
Using pass
for i in range(1, 6):
if i == 3:
pass
print(i)
1
2
3
4
5
12. break with while Loop
Example
i = 1
while i <= 10:
if i == 5:
break
print(i)
i += 1
1
2
3
4
13. continue with while Loop
Example
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
1
2
4
5
continue in a while loop,
make sure the loop-control variable is updated properly.
Otherwise, an infinite loop can occur.
14. Real-Life Examples
| Python Statement | Real-Life Example |
|---|---|
break |
You are searching for a book. Once you find it, you stop searching. |
continue |
You are checking students. If one student is absent, you skip that student and check the next student. |
pass |
A task has not been implemented yet, so you temporarily leave the block empty. |
15. Important Examination Points
-
breakterminates the nearest enclosing loop. -
continueskips the current iteration. -
passperforms no operation. -
breakdoes not move to the next iteration. -
continuemoves to the next iteration. -
passis commonly used as a placeholder.
16. Complete Comparison Table
| Feature | break | continue | pass |
|---|---|---|---|
| Terminates loop | Yes | No | No |
| Skips current iteration | No | Yes | No |
| Does nothing | No | No | Yes |
| Next iteration | No | Yes | Yes |
| Used in loops | Yes | Yes | Yes |
| Used as placeholder | No | No | Yes |
17. Quick Memory Trick
CONTINUE → SKIP THE CURRENT ITERATION
PASS → DO NOTHING
18. Summary
The break, continue, and
pass statements are important Python control
statements.
- break: Completely terminates the loop.
- continue: Skips the current iteration and proceeds to the next iteration.
- pass: Performs no operation and is generally used as a placeholder.
No comments:
Post a Comment