Python break and continue Statements
break and continue are used inside loops to control what happens next.
breakstops the loop completelycontinueskips the current loop step and moves to the next one
These statements work in both for loops and while loops. They are useful when you want more control over loop behavior without rewriting the whole loop.
Quick example
for number in range(1, 6):
if number == 3:
continue
if number == 5:
break
print(number)
# Output:
# 1
# 2
# 4
Use continue to skip the current loop step. Use break to stop the loop completely.
What this page teaches
By the end of this page, you will understand that:
breakstops a loop earlycontinueskips to the next loop iteration- Both can be used inside
forloops andwhileloops - They help control loop flow without changing the whole loop
If you are still getting comfortable with loops, see Python for loops explained and Python while loops explained.
What break does
break exits the nearest loop immediately.
That means:
- the loop stops right away
- any code after
breakinside that loop does not run - Python continues with the first line after the loop
Use break when the job is already done. For example, if you are searching for the first match, there is no reason to keep looping after you find it.
Simple break example
This example stops when the target value is found.
numbers = [4, 7, 9, 12, 15]
for number in numbers:
if number == 12:
print("Found 12")
break
print("Checked", number)
print("Loop ended")
Output
Checked 4
Checked 7
Checked 9
Found 12
Loop ended
Why it stops
The important line is:
if number == 12:
break
As soon as number becomes 12, the loop ends. It does not continue to 15.
What continue does
continue skips the rest of the current iteration.
That means:
- the loop does not stop
- Python jumps straight to the next iteration
- any code after
continuein that iteration does not run
Use continue when you want to ignore some values but keep looping through the rest.
Simple continue example
This loop skips one value but keeps going.
for number in range(1, 6):
if number == 3:
continue
print(number)
Output
1
2
4
5
Why 3 is missing
When number == 3, Python runs continue. That skips the print(number) line for that iteration.
The loop then moves on to 4 and 5.
break vs continue
The difference is simple:
break= stop the loopcontinue= skip one pass of the loop
Use break when:
- the task is finished
- you found what you were looking for
- there is no need to keep checking more values
Use continue when:
- you want to ignore certain values
- you want to filter items during a loop
- the loop should keep going after skipping one case
Here is a side-by-side example:
print("Using break:")
for i in range(1, 6):
if i == 3:
break
print(i)
print("Using continue:")
for i in range(1, 6):
if i == 3:
continue
print(i)
Output
Using break:
1
2
Using continue:
1
2
4
5
Using break and continue in while loops
Both statements also work in while loops.
The main thing to watch is the loop variable. In a while loop, you usually update the variable yourself. If that update does not happen, the loop may never end.
Here is a safe while loop with break:
count = 1
while count <= 5:
if count == 4:
break
print(count)
count += 1
Output
1
2
3
And here is a while loop with continue:
count = 0
while count < 5:
count += 1
if count == 3:
continue
print(count)
Output
1
2
4
5
In this example, count += 1 happens before continue, so the loop still makes progress.
If you want to review loop conditions first, see Python if statements explained and Python while loops explained.
Common beginner mistakes
These are some common problems with break and continue.
Using break when continue was needed
If you use break, the whole loop stops.
for i in range(1, 6):
if i == 3:
break
print(i)
This prints:
1
2
If you only wanted to skip 3, use continue instead.
Using continue when break was needed
If you use continue, the loop keeps going.
for i in range(1, 6):
if i == 3:
continue
print(i)
This prints:
1
2
4
5
If you wanted the loop to stop at 3, use break.
Placing code after break or continue and expecting it to run
Code after these statements in the same iteration will not run.
for i in range(3):
if i == 1:
continue
print("This will never print")
print(i)
The print("This will never print") line is skipped because continue already jumped to the next iteration.
The same idea applies to break.
Creating an infinite while loop by not changing the condition value
This is a very common mistake.
count = 0
while count < 5:
if count == 2:
continue
print(count)
count += 1
This loop becomes infinite when count == 2.
Why?
countis2continuerunscount += 1is skippedcountstays2- the loop condition stays true forever
A correct version updates the counter before continue can happen:
count = 0
while count < 5:
count += 1
if count == 3:
continue
print(count)
When to use each statement
Use break when:
- you are searching for the first match
- the loop should end as soon as something happens
- continuing would waste time
Use continue when:
- you want to skip unwanted values
- you are filtering items during a loop
- the loop should keep running after one special case
Do not use them if they make the code harder to read. In some cases, a simple if statement is clearer.
For example, this can be easier to read than using continue:
for number in range(1, 6):
if number != 3:
print(number)
Common causes of confusion
Beginners often get stuck because of one of these:
- confusing “stop the loop” with “skip this iteration”
- not realizing that
breakonly exits the nearest loop - forgetting that
continueskips the remaining code in the current iteration - using
continuein awhileloop before updating the counter
If your loop is not behaving as expected, add simple debug prints:
print(i)
print('before continue', i)
print('before break', i)
These help you see which values the loop is reaching and whether break or continue runs before later code.
FAQ
What is the difference between break and continue in Python?
break ends the loop completely. continue skips the current iteration and moves to the next one.
Can I use break in a for loop and a while loop?
Yes. break works in both for loops and while loops.
Does continue stop the loop?
No. It only skips the rest of the current iteration.
Why did my while loop become infinite after using continue?
The loop variable may not have been updated before continue ran, so the condition never changed.
Does break exit nested loops?
No. It only exits the nearest loop that contains it.