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

What is a Jump Statement?

A loop, left alone, always finishes what it started: every value gets its turn, and every round runs the whole body. A jump statement is how you overrule that from inside. There are two of them, they are one word each, and the only thing you have to keep straight is how far each one jumps.

1Why a loop would want to stop early

You are looking for a name in a list of forty. You find it at position three. The loop, being a loop, carries on and checks the other thirty-seven — patiently, pointlessly, every single time.

Or you are adding up a column of marks and one entry is -1, meaning the paper was never submitted. You do not want to stop the whole loop over it. You want to ignore that one and get on with the next.

Two different needs, two different statements — and they are exactly the two Python gives you:

StatementWhat it abandonsWhere it goes
breakthe rest of this round and every round after itthe first statement after the loop
continuethe rest of this round onlyback up to the loop header
Key Takeaway
A jump statement moves control somewhere other than the next line. break jumps out of the loop; continue jumps to the next round of it.

2The difference, in one loop

Here is the same six-round loop three times over. Only line 3 changes — first pass, which you met last lesson and which does nothing at all, then break, then continue. Watch what happens at the moment i is 4:

↪️ 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:
break
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.

Three words, three outputs. pass printed everything, break stopped at 3, and continue printed everything except 4. The loop, the condition and the print() were identical in all three.

Line 3 saysOutputRounds run
pass1 2 3 4 5 66 in full
break1 2 33 in full, then out
continue1 2 3 5 66 started, 1 cut short

3A jump is always inside an if

Look at where the jump sits in that program: under an if, never on its own. That is not a style choice. A break that is not inside an if runs on the very first round, so the loop stops before it has done anything:

pointless.py
# break with no condition — the loop runs exactly once
for i in range(1, 4):
    print(i)
    break

print('Loop over')
Output
1
Loop over

A jump statement answers the question “should I leave now?”, and only a condition can ask that question. So in practice you will always write them the same way: an if inside the loop, with the jump as its block.

4Neither one works outside a loop

break and continue are statements about a loop. Written anywhere else — even inside a perfectly good if — they mean nothing, and Python says so before the program runs:

no_loop.py
marks = 95

if marks > 90:
    break
Output
  File "no_loop.py", line 4
    break
    ^^^^^
SyntaxError: 'break' outside loop
no_loop2.py
marks = 95

if marks > 90:
    continue
Output
  File "no_loop2.py", line 4
    continue
    ^^^^^^^^
SyntaxError: 'continue' not properly in loop
Watch Out
Cannot be used in isolation. That is the phrase the exam uses, and this is what it means: a jump statement must be written inside the body of a loop. The if around it does not count as a loop — an if is not something you can leave early, because it only ever runs once anyway.

5Nothing after a jump ever runs

Once the program jumps, the lines after it in the same block are never reached. They are not a mistake Python will report — they simply sit there, forever unused:

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

Code like that last line is called unreachable code. Python does not complain, does not warn, and gives you no clue — which is why a student who puts the jump too early can stare at a program for a long time wondering why half of it seems to have no effect.

6Recap

Key Takeaway
Jump statements are the fifth and last kind of statement. Python has two: break leaves the loop altogether, and continue leaves only the current round. Both are keywords, both are single words, both are written inside an if in a loop body, and both are a SyntaxError outside a loop. Anything written after them in the same block is unreachable.
Note
The next two lessons take one each, with programs worth writing. Then a last one on else, which turns out to have a use with loops that only makes sense once you know break.
Quick Check

A loop is meant to stop as soon as a name is found. Which statement does that?

Quick Check

Where does continue send the program?

Quick Check

What happens if break is written inside an if that is not inside any loop?