Continue
Part of the Fundamentals section of Coddy's Python journey — lesson 38 of 77.
The continue statement stops the current iteration and continues to the next iteration. For example:
for i in range(3, 9):
if i == 5:
continue
print(i)The loop will iterate through the numbers from 3 to 8, and when it reaches i=5 it will skip that iteration and continue to the next one. The output is:
3
4
6
7
8Notice, number 5 is not in the output.
Challenge
BeginnerYou are given a code which prints the numbers from 1 to 20 (including).
Your task is to add if and continue statements so that only the even numbers will be printed (2, 4, 6, ...).
Cheat sheet
The continue statement skips the current iteration in a loop:
for i in range(3, 9):
if i == 5:
continue
print(i)
# Output: 3, 4, 6, 7, 8Try it yourself
for i in range(1, 21):
print(i)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2