Skip a loop with continue #
The continue statement skips the current item and moves on to the next item in the loop. Unlike break, the loop will continue until all items have been processed.
In the example below, we see that only odd numbers are printed since the print statement is skipped when the number is even.
numbers = [1, 2, 3, 4, 5, 6, 7 ,8 ,9, 10]
for number in numbers:
if number % 2 == 0:
continue
print(number)
print("This is called after the loop")
Here's the output of the example code above:
1
3
5
7
9
This is called after the loop
Exercise #
- Use
continueinsideforloop so that only even numbers are printed to the output.
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules