Python provides us with the continue and break statements to control the execution of a program inside a for loop or a while loop. This article discusses the continue vs break keyword in Python to understand the similarities and differences between the functioning of both these statements.
Python continue vs break Statement Summary
The following table contains the similarities and differences between the continue vs break statement in Python.
The continue Statement | The break Statement |
The continue statement is used to skip an iteration of a for loop or a while loop in Python. | The break statement is used to terminate the execution of a for loop or a while loop in Python. |
The continue statement can lead to an infinite loop when using a while loop if the statement to change the loop variable is written after the continue statement. | The break statement never causes an infinite loop. |
We cannot use the continue statement outside a for loop or a while loop. | We cannot use the break statement outside a for loop or a while loop. |
The continue statement can be executed multiple times in a for loop or while loop in Python. | The break statement can be executed only once inside a loop. After this, the execution of the loop will be terminated. |
Now, let us discuss all the basics of the continue and break keyword to understand all the points mentioned in the table.
What is the continue Keyword in Python?
The continue keyword in Python is used to skip the execution of statements in a for loop or while loop. Whenever the Python interpreter encounters a continue statement, it jumps the execution of the loop to the next iteration. To understand this, consider the following example.
for i in range(10):
print(i, end=":")
if i==5:
print("Will not print the square of 5")
continue
print(i**2)
Output:
0:0
1:1
2:4
3:9
4:16
5:Will not print the square of 5
6:36
7:49
8:64
9:81
In the above code, we have used the for loop to print numbers from 0 to 10 with their squares. In the output, you can observe that the loop doesn’t print the square for 5. This is due to the reason that when i==5, the continue statement inside the if block is executed. Due to this, all the remaining statements in the for loop are skipped and the execution goes to the next iteration of the loop.
Continue Keyword With For Loop in Python
As discussed above, you can use the continue keyword with a for loop in Python to skip the execution of the statements in the code in specific cases.
For example, suppose that we have a list of numbers. We need to print the square of the numbers in the list if they are multiple of 10. Otherwise, we need to print the number itself. We can use the continue statement to perform this task as shown below.
myList=[1,2,11,12,60,13,1234,2,10,30,45,110,15]
print("The list is:")
print(myList)
print("The output is:")
for number in myList:
if number%10==0:
print(number**2)
continue
print(number)
Output:
The list is:
[1, 2, 11, 12, 60, 13, 1234, 2, 10, 30, 45, 110, 15]
The output is:
1
2
11
12
3600
13
1234
2
100
900
45
12100
15
The continue Keyword With While Loop in Python
Just like a for loop, we can also use the continue statement with a while loop in Python. In the loop, when the interpreter encounters a continue statement, it will skip the following lines and move to the next iteration of the loop. To understand this, consider the following example.
count=0
while count<10:
count+=1
if count%3==0:
continue
print(count)
Output:
1
2
4
5
7
8
10
In the above code, we have printed the numbers from 1 to 10 except the multiples of 3. Whenever we get a multiple of 3, the condition in the if statement becomes True. Due to this, the continue statement inside the if block is executed and the lines below the continue statement in the while loop are skipped.
Suggested reading: The Zen of Python Explained With Examples
Using the continue statement in the while loop may lead to an infinite loop if we don’t write the code correctly. In the previous example, we have incremented the loop counter before the continue statement. Due to this, the counter is incremented even if the continue statement is executed.
However, if the statement to change the loop variable is written after the continue statement, the loop will run indefinitely as the statement for changing the loop variable will never be executed once the continue statement is executed.
To understand this, consider the following code.
count=0
while count<10:
if count%3==0:
continue
print(count)
count+=1
In the above code, when the variable count becomes 3, the condition inside the if statement becomes True. Hence, the continue statement is executed. Due to this, all the following statements are skipped and the loop variable isn’t changed. When the loop goes into the next iteration, the value in the loop variable hasn’t changed and the continue statement is executed again. This process runs indefinitely until we stop the program.
Hence, using the Python continue statement with the while loop might cause the loop to run indefinitely if the statement to change the loop variable is written after the continue statement.
Also, you cannot use the continue statement outside the for loop or while loop. If we try to use the continue statement outside a for loop or while loop, the program will run into an exception. You can observe this in the following example.
print("PFB")
continue
print("PythonForBeginners")
Output:
PFB
File "/tmp/ipykernel_7853/2505494057.py", line 2
continue
^
SyntaxError: 'continue' not properly in loop
In the above code, we have used the continue statement between two print statements without any for loop or while loop. Due to this, the program runs into a SyntaxError exception.
What is The break Keyword in Python?
The break statement in Python is used to stop the execution of a for loop or while. Once the break statement is executed, the execution of the program comes outside the loop and the next statement after the loop is executed. You can observe this in the following code.
for i in range(10):
print(i)
if i==5:
break
print("This is printed after the break statement")
Output:
0
1
2
3
4
5
This is printed after the break statement
In the above example, you can see that the execution of the for loop stops when the variable i becomes equal to 5. This is due to the reason that the break statement is executed once the condition in the if statement becomes True. Due to this, the execution of the for loop is terminated.
Similar to a for loop, we can also use the break statement to stop the execution of a while loop as shown below.
count=0
while count<10:
count+=1
print(count)
if count==5:
break
print("This is printed after the break statement")
Output:
1
2
3
4
5
This is printed after the break statement
Just like the continue statement, we cannot use the break statement outside for loop or while loop. Doing so will lead to an exception as shown below.
print("PFB")
break
print("PythonForBeginners")
Output:
PFB
File "/tmp/ipykernel_7853/643296189.py", line 2
break
^
SyntaxError: 'break' outside loop
Python continue vs break Statement: What Should You Use?
We use the continue statement when we just need to skip the execution of an iteration of a loop. On the other hand, we use the break statement to terminate the execution of a for loop or while loop in Python.
Conclusion
In this article, we discussed the basics of the continue and break statements in Python. We also discussed the differences between Python continue vs break statements. To learn more about Python programming, you can read this article on pandas map vs apply method in Python. You might also like this article on the pass keyword in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.