LambdaLabTM
Computer Science · Class 11 · Jump Statements
Types of StatementsJump⏱️ 12 min read

The continue Statement

continue abandons the rest of this round and nothing more. The loop itself is untouched: the pointer goes straight back up to the header, the next value is handed out, and the body starts again from the top. It is the difference between give up and skip this one.

1How it is written

shape.py
for value in values:
    statement
    if this one should be skipped:
        continue          <- jumps back up to the header
    statement             <- not run on a skipped round

next statement

Like break, it is a keyword and a single-word statement written inside a loop body, and like break it lives under an if — a bare continue would skip the rest of the body on every round, which makes the lines below it pointless.

2Printing 1 to 10, minus the multiples of 4

skip_fours.py
# print 1 to 10, but skip anything divisible by 4
for i in range(1, 11):
    if i % 4 == 0:
        continue
    print(i)
Output
1
2
3
5
6
7
9
10

4 and 8 are missing, and everything else is there. On those two rounds i % 4 == 0 was True, continue ran, and print(i) was skipped — but the loop went on to the next value as if nothing had happened.

Key Takeaway
All ten rounds started. Two of them did not finish. That is what separates continue from break, which would have printed 1, 2, 3 and then stopped for good.

3Step through it

The tracer again, this time on continue. The round where i is 4 is the one to watch: the pointer jumps from the jump line back up to the header, and 4 never reaches the output.

↪️ One word, three endings

Change the word on line 3 and step through. Watch what happens when i is 4.

i =
jump.py
loop headerif headerbody — the indented blockoutside
for i in range(1, 7):
if i == 4:
continue
print(i)
print('Loop over')
the six rounds
123456

Green means the value reached the screen. A crossed-out value is a round whose print(i) never ran.

output
nothing yet

The header works out range(1, 7) — the values 1 to 6 — and gets ready to hand them out one at a time.

4What it is really for: skipping the odd one out

Real data has gaps in it. A marks list might use -1 to mean the paper was never submitted — and that value must not be added to the total or counted as a paper:

marks.py
# -1 means the paper was not submitted
marks = [78, -1, 92, -1, 65]
total = 0
count = 0

for m in marks:
    if m == -1:
        continue
    total = total + m
    count = count + 1

print('Papers marked:', count)
print('Total marks:', total)
Output
Papers marked: 3
Total marks: 235

Two lines were skipped twice, and the other three rounds ran in full. A break here would have been a disaster: the loop would have stopped at the first -1 and the last three marks would never have been seen.

5Every continue can be written as an if

Here is something worth knowing before an exam asks: continue never adds anything Python could not already do. Turn the condition round, put the work inside it, and the continue disappears:

skip_fours_no_continue.py
# the same program written without continue
for i in range(1, 11):
    if i % 4 != 0:
        print(i)
Output
1
2
3
5
6
7
9
10

Identical output, one line shorter. continue said “if it is a multiple of 4, skip it”; this says “if it is not a multiple of 4, print it”. Same rule, stated the other way round.

Note
So when is continue the better choice? When the thing being skipped is one bad case and the work is long. The marks program above would need its whole body — both lines and any that come later — indented one level further inside an if m != -1:. Handling the odd case first and getting it out of the way keeps the main work at the left, where it is easier to read.

6The trap: continue in a while loop

In a for loop, continue is safe — the header hands out the next value by itself. A while loop has no header doing that. You update the variable, in the body — and continue skips the rest of the body.

Put the update before the skip and all is well:

while_continue.py
# the update comes first, so continue cannot skip it
i = 0

while i < 5:
    i = i + 1
    if i == 3:
        continue
    print(i)
Output
1
2
4
5

Put it after, and the program hangs. When i is 3, the continue jumps back to the condition without ever reaching i = i + 1, so i is 3 again, and again, for ever. It prints 0, 1 and 2 and then stands still — not ending, just never getting anywhere:

never_ends.py
# DO NOT RUN THIS — it never stops
i = 0

while i < 5:
    if i == 3:
        continue
    print(i)
    i = i + 1
Watch Out
Do not run that one in the playground. An endless loop in the browser freezes the tab it is running in, and this page is that tab. In IDLE you can stop a runaway program with Ctrl + C; here the only cure is to reload.
Tip
In a while loop, write the update line as high in the body as you can — ideally the first statement. Then no continue can ever jump over it.

7Nothing after it runs either

Just like break, anything written after a continue in the same block is unreachable — the jump has already happened:

unreachable.py
for i in range(1, 4):
    continue
    print('this line never runs')

print('done')
Output
done

8Try it

Change continue to break in the program below and run it again. One adds four readings and reaches 59; the other gives up at the first negative number and reaches 12.

skip_negatives.py

9Recap

Key Takeaway
continue skips the current round of the loop and goes on to the next one. The loop is not ended, no values are lost, and only the statements below the continue in that round are missed. It is a keyword, it must be inside a loop, anything after it in the same block is unreachable — and in a while loop it will happily skip your update line and hang the program.
Quick Check

A loop runs for i in range(1, 6). Its body skips with continue when i == 3, and the line after that check prints i. What is printed?

Quick Check

Which statement would you use to ignore one bad value in a list and carry on with the rest?

Quick Check

Why can continue hang a while loop but not a for loop?