LambdaLabTM
Computer Science · Class 11 · Practice Programs
PracticeSkip a round⏱️ 15 min read

continue Statement Programs

continue is the opposite half of the pair. break says we are done here; continue says not this one — next. The rest of the round is skipped, the loop itself is untouched, and the next value is handed out as usual.

The lesson these programs practiseThe continue Statement

1Program 1 — print 1 to 15, skipping the multiples of 3

📋 The problem

Print the numbers from 1 to 15, but leave out every multiple of 3.

skip_threes.py
# print 1 to 15, but skip every multiple of 3

for i in range(1, 16):
    if i % 3 == 0:
        continue

    print(i)
Output
1
2
4
5
7
8
10
11
13
14

Ten numbers printed out of fifteen handed out. The loop ran all fifteen rounds — nothing was cut short — but on five of them continue jumped straight back to the header before print(i) could run.

Key Takeaway
continue skips the rest of the body, not the rest of the loop. Swap it for break in this program and the output is 1, 2 — and then nothing at all, for ever. One word, and the difference between ten lines and two.

2Program 2 — total only the positive numbers

📋 The problem

A list holds a mix of positive and negative numbers. Add up only the positive ones.

Input
what we ask the user for
  • a list of numbers
Process
what we work out
  • skip any number below zero
  • add everything else to a total
Output
what we show
  • the total of the positive numbers
sum_positive.py
# add up only the positive numbers in the list

numbers = [12, -5, 8, -2, 30, -14, 7]
total = 0

for n in numbers:
    if n < 0:
        continue

    total = total + n

print('The positive numbers add up to', total)
Output
The positive numbers add up to 57

12 + 8 + 30 + 7 = 57. Notice that total is still started above the loop and printed after it — continue changes which rounds contribute, and nothing else about the shape of a loop.

Tip
The same program without continue. Turn the test round — if n >= 0: total = total + n — and the answer is identical. Both are right. continue reads better when the thing being skipped is an exception to the job (bad data, absentees), and an if reads better when both halves are ordinary cases.

3Program 3 — average the marks of the students who sat the test

📋 The problem

A list of marks uses -1 to mean the student was absent. Print how many sat the test and their average — the absentees must not drag it down.

average_present.py
# average the marks, skipping the students who were absent (-1)

marks = [72, -1, 65, 88, -1, 54]
total = 0
count = 0

for m in marks:
    if m == -1:
        continue

    total = total + m
    count = count + 1

print('Students present:', count)
print('Average mark:', total / count)
Output
Students present: 4
Average mark: 69.75
if m == -1:

-1 is a marker, not a mark — no test gives minus one. A value used this way is called a sentinel, the same idea as the 0 that ended the while loop on an earlier page.

continue

Skips both of the lines below it for this round. That matters twice over: the absent student is left out of the total AND out of the count.

print('Average mark:', total / count)

Divided by count, not by len(marks). Six marks were handed out and only four counted, so len() would give 46.5 — an average of nothing real.

Watch Out
count is the whole reason this program is correct. Dividing by len(marks) is the mistake to look for, and it gives an answer that looks perfectly plausible. The rule: if a loop skips rounds, then anything you divide by has to be counted inside the loop, past the continue.

4Program 4 — write a word without its vowels

📋 The problem

Ask for a word and print it again with every vowel left out.

drop_vowels.py
# build the same word again, leaving the vowels out

word = input('Enter a word: ')
without_vowels = ''

for letter in word:
    if letter.lower() in 'aeiou':
        continue

    without_vowels = without_vowels + letter

print('Without its vowels:', without_vowels)
Output
Enter a word: education
Without its vowels: dctn

The collector from the for loop page, with one round in five skipped. letter.lower() is only used for the test — the letter added to the collector is the original one, so a capital letter in the word stays a capital.

drop_vowels.py

5Program 5 — continue in a while loop

📋 The problem

Print 1 to 12, skipping the multiples of 5 — using a while loop rather than a for.

This is the one place continue is genuinely dangerous, and it is worth meeting on purpose. Here is the version that works:

while_continue.py
# skip the multiples of 5 — with the counter moved to the top of the body

i = 0

while i < 12:
    i = i + 1

    if i % 5 == 0:
        continue

    print(i)
Output
1
2
3
4
6
7
8
9
11
12

Now move the counter to the bottom of the body, where it looks more natural, and read what happens:

while_continue_broken.py
i = 1

while i <= 12:
    if i % 5 == 0:
        continue      <- jumps straight back up to the header...
    print(i)
    i = i + 1         <- ...so on that round this line never runs
Watch Out
continue will happily jump over the line that moves the loop on. That program prints 1, 2, 3, 4 and then hangs. On the round where i is 5 the continue skips the update, so i stays 5 — the condition is still true, the test is still true, and it skips again, for ever. We ran it with a counter cap rather than let it run: after 50 rounds i was still 5. This is why the working version above does its i = i + 1 first.
Key Takeaway
The fix is a rule: in a while loop, write the update as high in the body as it will go. Above every continue, so no path through the body can miss it. A for loop never has this problem, because its counter is handed out by the header and nothing in the body can skip it — which is one more reason to reach for for when the count is known.

6Program 6 — break and continue in one loop

📋 The problem

A list of temperature readings has two kinds of bad value: 0 means the reading was missed, and a negative number means the sensor is broken and nothing after it can be trusted. Skip the first kind and stop at the second.

both_jumps.py
# continue skips a round, break ends the loop

readings = [23, 0, 27, 25, -1, 29]

for r in readings:
    if r == 0:
        print('Missing reading, skipping')
        continue

    if r < 0:
        print('Broken sensor, stopping')
        break

    print('Temperature:', r)

print('Report ended')
Output
Temperature: 23
Missing reading, skipping
Temperature: 27
Temperature: 25
Broken sensor, stopping
Report ended

Read the output against the list and the difference is right there. At 0 the loop carried on and 27 and 25 still arrived. At -1 it stopped, and 29 was never looked at — even though it is a perfectly good reading. That is the choice you are making when you pick one word over the other.

Key Takeaway
Two questions tell them apart. Is the rest of the data still worth reading? Then continue. Is there nothing useful left to do? Then break. Both write the same way — a test, and one word inside it.

7Recap

It skips the rest of the body

Only this round, and only the lines below it. The loop goes back to its header and carries on with the next value.

Count inside the loop

If rounds are being skipped, len() no longer says how many were used. Anything you divide by must be counted past the continue.

In a while loop, update first

continue jumps over everything below it, the update line included. Write the update above every continue or the loop can hang.

It can always be avoided

Turning the test round gives the same answer. Use continue when the skipped case is an exception to the job, not one of two equal halves.

✍️ Now write these yourself
  1. 1

    Print 1 to 30, skipping every multiple of both 3 and 5.

    Hint · One if with an and in it, then continue.

  2. 2

    Count the consonants in a word by skipping the vowels rather than collecting the letters.

    Hint · Program 4 with a counter instead of a string — and remember a space is not a consonant.

  3. 3

    Total a shopkeeper's sales, skipping any entry recorded as 0 because the day was a holiday, and print how many days actually traded.

    Hint · Two collectors — a total and a count — both incremented past the continue.

  4. 4

    Print the numbers 1 to 20 that are neither even nor multiples of 7.

    Hint · Two separate tests, each with its own continue, is easier to read than one long condition.

  5. 5

    Read marks until the user types -2 to finish, ignoring any mark above 100 as a typing mistake.

    Hint · break for the -2, continue for the impossible mark — and in a while, read the next value before either of them.

Quick Check

for i in range(1, 16) with if i % 3 == 0: continue — how many numbers are printed?

Quick Check

A loop skips absent students with continue and then divides the total by len(marks). What is wrong?

Quick Check

In a while loop, why must the update line sit above the continue?