← Python for Beginners
0 of 27 lessons done0%

Loops · Lesson 14 of 27

break and continue

Two keywords control a loop from the inside:

  • break stops the loop completely.
  • continue skips the rest of this round and goes to the next one.
for i in range(1, 10):
    if i == 5:
        break
    print(i)          # 1 2 3 4
for i in range(1, 6):
    if i == 3:
        continue
    print(i)          # 1 2 4 5   (3 is skipped)

Your task

Read a number n. Print every number from 1 to n, skipping numbers divisible by 3. Use continue.

For n = 10, print 1 2 4 5 7 8 10, one number per line.