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:
| Statement | What it abandons | Where it goes |
|---|---|---|
break | the rest of this round and every round after it | the first statement after the loop |
continue | the rest of this round only | back up to the loop header |
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:
Change the word on line 3 and step through. Watch what happens when i is 4.
Green means the value reached the screen. A crossed-out value is a round whose print(i) never ran.
nothing yetThe 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 says | Output | Rounds run |
|---|---|---|
pass | 1 2 3 4 5 6 | 6 in full |
break | 1 2 3 | 3 in full, then out |
continue | 1 2 3 5 6 | 6 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:
# break with no condition — the loop runs exactly once
for i in range(1, 4):
print(i)
break
print('Loop over')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:
marks = 95
if marks > 90:
break File "no_loop.py", line 4
break
^^^^^
SyntaxError: 'break' outside loopmarks = 95
if marks > 90:
continue File "no_loop2.py", line 4
continue
^^^^^^^^
SyntaxError: 'continue' not properly in loopif 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:
for i in range(1, 4):
print(i)
break
print('this line never runs')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
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.else, which turns out to have a use with loops that only makes sense once you know break.A loop is meant to stop as soon as a name is found. Which statement does that?
Where does continue send the program?
What happens if break is written inside an if that is not inside any loop?