continue
The continue
keyword in Python is used inside loops to bypass the remaining code in the current iteration and immediately begin the next one. When Python encounters continue
, it jumps to the next iteration without executing the remaining statements in the loop body. This allows certain conditions to bypass parts of the loop without exiting it completely. It is useful for filtering out unwanted values, skipping invalid data, or focusing on specific cases within loops.
Syntax
continue
Parameters:
The continue
statement does not take any parameters.
Return value:
The continue
statement does not return any value. It simply skips to the next iteration of the nearest enclosing loop.
Note: Unlike
break
,continue
does not exit the loop - it only skips the current iteration.
Example: Using continue
in a while
Loop
This example demonstrates how the continue
statement skips the iteration when the count reaches 3, preventing it from being printed:
count = 0while count < 5:count += 1if count == 3:continueprint(count, end = ' ')
The output of this code is:
1 2 4 5
Codebyte Example: Skipping Even Numbers Using continue
This codebyte example defines a function that prints only odd numbers from 0-9 by using the continue
statement to skip even numbers in a for
loop:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours