Python continue
Python continue is a loop control statement.
Python continue is used to skip further statements in a loop, and continue with the further iterations of the loop.
Syntax of Python continue
Following is the syntax of Python continue statement.
continue
Example 1 – Python continue in for loop
In the following example, we will use continue statement inside Python For loop to skip the statements inside the loop for this iterationand continue with the rest of iterations.
example.py – Python Program
for number in [1, 2, 3, 5, 6, 10, 11, 12]: if number==6: continue print(number) print('Bye')Try Online
Output
1 2 3 5 10 11 12 Bye
Only those statements that are after continue statement inside the loop are skipped for that specific iteration and continued with subsequent iterations.
Example 2 – Python continue statement with while loop
In the following example, we will use continue statement inside Python While Loop.
example.py – Python Program
i=1 while i < 11: if i==6: i=i+1 continue print(i) i=i+1 print('Bye')Try Online
Output
1 2 3 4 5 7 8 9 10 Bye
Be careful with the update before continue statement. It may result in infinite loop.
Example 3 – Python continue statement outside loop statement
In the following example, we will try to use continue statement outside for or while loop statement.
example.py – Python Program
i=1 if i==2: continue print('Bye')Try Online
Output
File "example1.py", line 4 continue ^ SyntaxError: 'continue' not properly in loop
You will get SyntaxError
with the message 'continue' not properly in loop
if you try to use continue statement outside for or while loop.
Conclusion
In this Python Tutorial, we learned how to use Python continue statement with for and while loops.