AI Features

Solution: Skip Even Numbers

We'll cover the following...

This program prints all odd numbers from 1 to 10 using a for loop and the continue statement.

  • range(1, 11) generates numbers from 1 to 10 (the end value 11 is not included).

  • The if number % 2 == 0 line checks if a number is even using the modulo operator %.

    • % gives the remainder after division.

    • If the remainder is 0 when dividing by 2, the number is even.

  • continue tells Python to skip the rest of the loop for that number and move to the next one.

  • So, only odd numbers (those not skipped) get printed.

Python
for number in range(1, 11):
if number % 2 == 0:
continue
print(number)