Python break and continue Statement
As we all know, using for loops and while loops enables you to automate and repeat processes efficiently; however,
Instances occur where external factors may impact the performance of our application. When this happens, you could do these things:
- exit the loop entirely
- skip a portion of the loop before continuing to
- ignore the external element when this occurs
But with the use of break, continue, and pass statements, these operations can be executed.
With that being said, let’s discover how to break a loop in Python.
How to break a for loop in Python?
The break statement is used within loops to forcefully terminate the loop’s execution when a specific condition is satisfied.
It is commonly employed within loops to stop iterating over elements once the desired condition is met.
Syntax of the break statement
The syntax of the break statement is straightforward:
for element in iterable:
# Code block
if condition:
break # Exit the loop if the condition is met
# Rest of the code
Let’s look at an example of how to use a break statement in a for loop:
num = 0
for number in range(10):
if number == 4:
break
print('Number is ' + str(number))
print('Out of loop')
In this sample program, the variable “num” is initialized to 0. A “for” loop is created to execute as long as “num” is less than 10.
Inside the loop, there is an “if” statement that checks if “num” is equal to 4, and if so, the loop will break.
With each iteration of the loop, a “print()” statement is executed until the loop breaks due to the “break” statement.
After the loop, a final “print()” statement marks the end of the program.
The output of the example code:
Number is 0
Number is 1
Number is 2
Number is 3
Out of loop
What is the use of break and continue in Python?
The use of break and continue in Python is that their statements can change the typical flow of a loop.
The main function of loops is to iterate over a code of block until the expression of the test is false.
However, sometimes we want to stop the current iteration of the whole loop without checking the expression.
This is when the break and continue statement is used.
Break statement Python
The Break statement in Python will terminate the loop when executed.
The statement that comes immediately after the loop’s body is the one that receives and controls statements of the program.
On the other hand, nested loops (one loop inside another) will be terminated by the break statement if it is inside the innermost loop.
Syntax of break:
break
Example of Break statement Python:
for val in "python":
if val == "x":
break
print(val)
print("The end")
Output:
p y t h o n The end
Explanation:
In this program, the “Python” sequence is iterated. Upon determining that the letter is x, we exit the loop.
Thus, we observe in our output that all letters up until x are written. Following this, the loop ends.
Python continue statement
The continue statement bypasses the remainder of the code within a loop solely for the current iteration.
The Loop moves on to the following iteration without terminating.
Syntax of Python continue statement:
continue
Example:
for val in "python":
if val == "x":
continue
print(val)
print("The end")
Output:
p y t h o n The end
Explanation:
This program is the same as the above example, except the break
the statement has been replaced with continue
.
We continue with the loop if the string is x, not executing the rest of the block.
Hence, we see in our output that all the letters except x get printed.
FAQs (Frequently Asked Questions)
The break statement can be used in both while and for loops. If you are using nested loops, the break statement terminates the innermost loop execution and starts executing the next line of code after the block.
The Python break statement immediately terminates the loop entirely.
To learn more about Python, visit our website to see a bunch of Python tutorials that can definitely help you.
Conclusion
In conclusion, a Python break for loop is crucial, especially when you want to terminate the loop when in instances affecting the program flow.
Along with the Python continue statement, which skips the remainder of the loop code and continues to the iteration without terminating the program.