How to use break & continue in Python for loop? for loop iterates blocks of code until the condition is False
. Sometimes you need to exit a loop completely or when you want to skip a current part of the python for loop and go for the next execution without exiting from the loop. Python allows break
and continue
statements to overcome such situations and you can be well controlled over your loops.
break
– Statement is used to exit from the for and while loops. Post exit it executes the immediately following statement of for/while statement it exited from.
continue
– Skips the current execution and continues with the next iteration of for & while loops.
In this article, you will learn the usage of break and continue statements with Python for loop examples.
1. Quick Examples Using for Loop continue and break
Following are quick examples of how to use a break and continue blocks with python for loop.
courses=["java","python","pandas","sparks"]
# Example 1 : Using continue in for loop
for x in courses:
if x=='pandas':
continue
print(x)
# Example 2 : Using break in for loop
for x in courses:
print(x)
if x == 'pandas':
break
# placed the print() after the break
for x in courses:
if x == 'pandas':
break
print(x)
# Using break statement inside the nested loop
ourses1=["java","python"]
courses2=["pandas","java","python"]
for x in courses1:
for y in courses2:
if x==y:
break
print(x ,y)
# Using else block with a break statement
for x in courses:
print(x)
if x == 'pandas':
break
else:
print("Task finished")
2. Using Python continue Statement
Using the continue
statement we can skip the current iteration of the loop and continue
for the next iteration of the loop.
# Skip the loop using continue statement
courses=["java","python","pandas","sparks"]
for x in courses:
if x == 'pandas':
continue
print(x)
Here, I have taken courses
as a list, which is iterated using for loop with the continue
statement. Here as you see continue
statement is used within the if condition. If the loop reaches the 'pandas'
, the condition in the if statement becomes true, so the continue
statement will execute and skips the current iteration(pandas) and go for the next iteration.
Note: continue
statement doesn’t exit the for/while loop.
Yields below output
java
python
sparks
3. Using Python break Statement
Sometimes you would like to exit from the python for
/while
loop when you meet certain conditions, using the break
statement you can exit the loop when the condition meets. The example is given below.
With the break
statement, you will early exit from the loop and continue the execution of the first statement after the loop.
# Exit the loop using break statement
courses=["java","python","pandas","sparks"]
for x in courses:
print(x)
if x == 'pandas':
break
In the above example, I have taken the courses
variable as a list which is iterated using for loop. We have applied a break
statement based on x == 'pandas'
condition. When the iteration reaches the pandas
value, the for loop will exit using a break
statement.
The above example exits for loop when x
value equals to pandas
.
java
python
pandas
Let’s place the print() function after the if statement
.
# placed the print() after the break
courses=["java","python","pandas","sparks"]
for x in courses:
if x == 'pandas':
break
print(x)
Yields below output
java
python
Notice that for loop exited after reaching x
value ‘pandas
‘.
4. Nested for Loop Using break Statement
If a loop presents inside the body of another loop is called a nested loop
. The inner loop will be executed n number of times for each iteration of the outer loop. The example is given below.
If the break
statement is inside a nested loop, the break
statement will end the innermost loop and the outer loop continue executing.
let’s take an example for better understanding.
# Using break statement inside the nested loop
ourses1=["java","python"]
courses2=["pandas","java","python"]
for x in courses1:
for y in courses2:
if x==y:
break
print(x ,y)
In the above example, the inner loop will be executed three times(‘pandas’, ‘java’, ‘python’) for each iteration of the outer loop.
Possibilities of the outer loop for the first iteration:
-
java, pandas
java, java
in this case, the condition satisfies so the innermost loop ends. Then the control goes to the outer loop.
Possibilities of the outer loop for the second iteration :
python, pandas
python, java
python, python
in this case, the condition satisfies so the innermost loop ends.
Yields below output.
java pandas
python pandas
python java
5. For Loop else Block With break Statement
Python allows the else
keyword with for
loop. The else block is optional and should be after the body of the loop. The statements in the else block will execute after completing all the iterations of the loop. The program exits the loop only after the else block is executed.
As you learned above, you can use the break statement to exit the loop. In such cases, the else part will not be executed.
# Using else block with break statement
courses=["java","python","pandas","sparks"]
for x in courses:
print(x)
if x == 'pandas':
break
else:
print("Task finished")
Yields below output
java
python
pandas
Conclusion
In this article, you have learned how to control the for
loop using the continue
and break
statement in Python. Along with this, you have learned nested loops
using a break statement and the behavior of else
block in a for loop with the break statement.
Happy Learning !!
Related Articles
- For Loop with If Statement in Python
- For Loop Break Statement in Python
- For Loop Iterate Over an Array in Python
- How to Perform Decrement for Loop in Python
- How to Increment for Loop in Python
- For Loop Enumerate in Python
- Get Counter Values in a for Loop in Python?
- Access Index in for Loop in Python