Python break
and continue
Statements
In Python, loops (like for
and while
) are essential constructs for repeating a block of code. But sometimes, you might want to control the flow of the loop more precisely. That’s where the break
and continue
statements come in. They allow you to alter the normal behavior of a loop and provide more flexibility in your code.
In this blog post, we will explore what the break
and continue
statements are, how to use them, and provide examples that demonstrate their use cases.
break
Statement?The break
statement is used to exit from a loop (either for
or while
) prematurely, stopping further iterations. Once break
is encountered, the loop terminates, and the program continues with the next statement after the loop.
break
:
for item in sequence:
if condition:
break
# Code to execute
The break
statement is most commonly used when a specific condition is met, and you want to stop the loop early.
break
in a for
Loop
# Example: Using break in a for loop
for i in range(10):
if i == 5:
print("Breaking the loop at i =", i)
break
print(i)
Output:
0
1
2
3
4
Breaking the loop at i = 5
In this example:
i = 0
and iterates through the range.i
equals 5, the break
statement is executed, and the loop terminates immediately, printing the message and stopping further iterations.continue
Statement?The continue
statement is used to skip the current iteration of the loop and proceed with the next iteration. Unlike break
, which exits the loop entirely, continue
only skips the current iteration and the loop continues to the next one.
continue
:
for item in sequence:
if condition:
continue
# Code to execute
The continue
statement is typically used when you want to skip certain values or conditions but continue processing the rest of the sequence.
continue
in a for
Loop
# Example: Using continue in a for loop
for i in range(5):
if i == 3:
print("Skipping i =", i)
continue
print(i)
Output:
0
1
2
Skipping i = 3
4
In this example:
i
equals 3, the continue
statement is executed, which skips printing the value 3
but continues with the next iteration (i.e., 4
).break
and continue
in while
LoopsBoth break
and continue
can also be used in while
loops to control the flow of iteration, just like in for
loops. The syntax remains the same.
break
in a while
Loop
# Example: Using break in a while loop
count = 0
while count < 5:
if count == 3:
print("Breaking the loop at count =", count)
break
print(count)
count += 1
Output:
0
1
2
Breaking the loop at count = 3
In this example:
while
loop iterates as long as count
is less than 5.count
equals 3, the break
statement stops the loop.continue
in a while
Loop
# Example: Using continue in a while loop
count = 0
while count < 5:
count += 1
if count == 3:
print("Skipping count =", count)
continue
print(count)
Output:
1
2
Skipping count = 3
4
5
In this case:
count
on each iteration.count
is 3, the continue
statement is executed, skipping the print statement for count = 3
and moving on to the next iteration (count = 4
).break
and continue
TogetherIt is possible to use both break
and continue
in the same loop. For example, you may want to continue with the next iteration when a certain condition is met, but also break out of the loop if another condition is satisfied.
break
and continue
in the Same Loop
# Example: Using both break and continue in the same loop
for i in range(10):
if i == 2:
print("Skipping i =", i)
continue
if i == 8:
print("Breaking the loop at i =", i)
break
print(i)
Output:
0
1
Skipping i = 2
3
4
5
6
7
Breaking the loop at i = 8
In this example:
i == 2
, the continue
statement skips that iteration.i == 8
, the break
statement is executed, stopping the loop entirely.Let's combine both break
and continue
to search for a specific value in a list.
# Example: Searching for a value in a list
items = ['apple', 'banana', 'cherry', 'date', 'elderberry']
search_term = 'cherry'
for item in items:
if item == 'banana': # Skip bananas
continue
if item == search_term:
print(f"Found {search_term}!")
break
print(f"Checked {item}")
Output:
Checked apple
Checked banana
Found cherry!
In this example:
banana
using the continue
statement.search_term
(in this case, "cherry"), it prints a message and exits the loop using break
.break
and continue
?break
: When you want to exit the loop entirely based on a specific condition.
continue
: When you want to skip the current iteration of the loop and continue with the next iteration.