Python Control Statements
Difference between Break and Continue in Python
Learn Difference between Break and Continue in Python with clear explanations, Python examples, and practical programming guidance.
What is the difference between break and continue?
Both statements change the normal flow of a loop. break ends the nearest loop completely, while continue skips only the current iteration and lets the loop process the next item.
The break statement
Use break when the loop should stop as soon as a condition is met:
for number in range(1, 11):
if number % 2 == 0:
break
print(number)Output
The loop prints 1, then stops when it reaches the first even number, 2. No later iterations run.
The continue statement
Use continue when one item should be ignored but the loop should continue:
for number in range(1, 11):
if number % 2 == 0:
continue
print(number)Output
3
5
7
9
Even numbers are skipped, but the loop continues until all numbers have been processed.
Comparison table
| Feature | break | continue |
|---|---|---|
| Purpose | Stops the nearest loop. | Skips the current iteration. |
| Later iterations | Do not run. | Continue to run. |
| Control moves to | The first statement after the loop. | The next iteration of the same loop. |
| Typical use | Stop after finding a result. | Ignore invalid or unwanted items. |
| Works with | for and while loops. | for and while loops. |
Find the first matching item with break
Once the requested item is found, there is no need to inspect the remaining list:
languages = ["Java", "Python", "Go", "Rust"]
for language in languages:
if language == "Python":
print("Found:", language)
breakOutput
Ignore invalid values with continue
Continue processing valid values while skipping negative numbers:
numbers = [10, -2, 5, -8, 7]
for number in numbers:
if number < 0:
continue
print(number)Output
5
7
Using them in a while loop
When using continue in a while loop, update the counter before continuing. Otherwise, the condition may never change and the program may run forever:
number = 0
while number < 5:
number += 1
if number == 3:
continue
print(number)Output
2
4
5
break, continue, and pass
| Statement | Effect |
|---|---|
break | Ends the nearest loop immediately. |
continue | Skips the rest of the current iteration and starts the next one. |
pass | Does nothing and acts as a placeholder. |
Key points for beginners
- Use
breakto stop searching or processing. - Use
continueto filter or ignore selected values. - Neither statement is used as a replacement for a condition; both normally appear inside an
ifblock. - In nested loops, each statement affects only the nearest loop.
Further Reading
Continue learning with these related Python tutorials:
Continue learning
