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.
1Program 1 — print 1 to 15, skipping the multiples of 3
Print the numbers from 1 to 15, but leave out every multiple of 3.
# print 1 to 15, but skip every multiple of 3
for i in range(1, 16):
if i % 3 == 0:
continue
print(i)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.
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
A list holds a mix of positive and negative numbers. Add up only the positive ones.
- a list of numbers
- skip any number below zero
- add everything else to a total
- the total of the positive numbers
# 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)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.
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
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 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)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.
continueSkips 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.
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
Ask for a word and print it again with every vowel left out.
# 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)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.
5Program 5 — continue in a while loop
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:
# 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)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:
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 runscontinue 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.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
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.
# 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')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.
continue. Is there nothing useful left to do? Then break. Both write the same way — a test, and one word inside it.7Recap
Only this round, and only the lines below it. The loop goes back to its header and carries on with the next value.
If rounds are being skipped, len() no longer says how many were used. Anything you divide by must be counted past the continue.
continue jumps over everything below it, the update line included. Write the update above every continue or the loop can hang.
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.
- 1
Print 1 to 30, skipping every multiple of both 3 and 5.
Hint · One
ifwith anandin it, thencontinue. - 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
Total a shopkeeper's sales, skipping any entry recorded as
0because 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
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
Read marks until the user types
-2to finish, ignoring any mark above 100 as a typing mistake.Hint ·
breakfor the-2,continuefor the impossible mark — and in awhile, read the next value before either of them.
for i in range(1, 16) with if i % 3 == 0: continue — how many numbers are printed?
A loop skips absent students with continue and then divides the total by len(marks). What is wrong?
In a while loop, why must the update line sit above the continue?