Loops in programming are used for executing a particular block of code a number of times. This code may run until a certain condition is False. But you might need to end the loop execution or the current iteration at a point without checking the condition. Python offers you two ways to do this - by using the break and continue statements.
Continue Statement
In python continue statement terminates all the remaining iteration and move the control back to the beginning of the loop for the next iteration.
The continue statement can be used in both while and for loops. Here in this article we have explained continue statement examples with for and while loop.
Flow Diagram
Continue Statement with for Loop
Example with for loop:
# Python program to explain continue statement with for loop
string1 = "Stechies"
# Continue with for loop
for value in string1:
# Check if string value is equal to letter 'e'
if value == 'e':
# If Letter is equal to 'e' next statement will skip
continue
# Skip statement after continue
print('This is continue block: ',value)
# Print the next iteration
print("Value: ",value)
# Outside the for loop
print("Outside for Loop")
Output:
Value: S
Value: t
Value: c
Value: h
Value: i
Value: s
Outside for Loop
Explanation
In the first line, a variable called string1 is initialized with the value “Stechies”. Then a for loop is used for iterating over every character in the variable string1. A variable called value is used for this. An if statement checks whether the value variable is equal to the character “e”. When the condition is True, the continue statement skips the next line after it that is print('This is continue block: ', value).
This throws the control out of the if statement and the next character is printed by a print() method. The line of code print("Value: ",value) prints the next character of the string.
When the loop finishes its execution, the string “Outside for loop” is printed to the screen.
Continue Statement with While Loop
Example with while loop:
#Python program to explain continue statement with while loop
i = 0
# Continue with while loop
while (i <= 10):
i = i + 1
# Check if value of i variable is equal to 5
if i == 5:
# If value is equal to 5 next statement will skip
continue
# Skip statement after continue
print("This is continue block:", i)
# Print the next iteration
print("Value of i:", i)
# Outside the for loop
print("Outside While Loop")
Output:
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 6
Value of i: 7
Value of i: 8
Value of i: 9
Value of i: 10
Value of i: 11
Outside While Loop
Explanation
A variable i is initialized with the value 0. Then a while loop checks whether the value of this variable is less than or equal to 10. As long as the condition is True, the variable i is incremented by 1. Then an if statement checks whether this variable is equal to 5. If the condition is True, the continue statement in the next line terminates the loop.
The line print("Value of i:", i) is executed next that prints the next character of the string. The last string “Outside While loop” is printed when the loop completes execution. So as a result, all the numbers less than 10 except the number 5 are printed to the screen.
The continue statement ends a loop iteration and does not execute any remaining lines of code after it. So use it carefully, as some important operations may not get executed - such as incrementing a loop variable. If you want to terminate the loop and move on to the code immediately after the loop, you can use the break statement.