How to use Python Break Continue statements?
In this short tutorial, we look at how the python break, continue statements affect the flow of a loop and how you could effectively use them in your programs.Table of Contents
- Why do we use python break, continue, and other control statements?
- Python break statement
- Python continue statement
- Closing thoughts
- Other Related Concepts
Why do we use python break, continue, and other loop control statements?
Loops in python are used to iterate over or repeat a command multiple times. To help us control the flow of these loops, Python provides a few control statements.Say you would want to skip a particular iteration or exit the loop when a particular condition is met. Python lets us perform these tasks by using Python break, continue, and other control statements.
In this tutorial, we look at the two most commonly used control statements; Python
break
and continue
. Python break statement
Afor
loop iterates for a particular number of times and a while
runs until a condition is true. However, what if the use case requires you to break the loop beforehand? This is where the Python break statement comes in handy. The
break
statements are your way of asking the loop to stop and execute the next statement. Python break statements are mostly used along with an If statement. This is because most use cases would require you to break the flow only when a particular condition is met.Note: Although Python break exits the loops, it only exits the loop it is placed in, so while using a nested loop only the loop containing the
break
statement would be exited.Syntax:
Break
Code:
for char in "flexiple":
if char == 'p':
break
print(char)
print("end")
Output:
f
l
e
x
i
end
As shown above, the loop runs 5 times and when if
is true, the Python break statement runs and exits the loop.Although we used a
for
loop in our code example, break
can be used in a while as well. One of the most common use cases of the Python break is to look for and exit the loop when the word "exit" is entered.Python Continue Statement
Unlike the Python break, the continue statement stops the flow but returns the flow to the beginning of the loop. It does not process the rest of the code for a particular iteration and restarts the flow for the next iteration.Python continue is used to skip a particular iteration. And similar to Python break, continue is also used when a condition is met.br>
Syntax:
continue
Code
for char in "flexiple":
if char == 'p':
continue
print(char)
print("end")
Output:
f
l
e
x
i
l
e
end
As you can see from the output Python continues, skips the iteration for "p" and executes the rest of the code. And similar to the break statement, continue can also be used with both for and while loops. Closing thoughts:
Even though the python break and continue statements may seem quite straightforward it will require some practice to get the hang of it.Practicing them using a while true loop would help you get a better understanding of how the flow can be changed. However, remember to insert a break before running it or you would create an infinity loop.
Once you have properly understood this you can have a look at the Python pass statement which is another loop control statement.