The break Statement
break ends the loop it is written in — not this round, the whole loop. Whatever was left of the body is abandoned, the values that were still to come are never handed out, and the program carries on from the first statement after the loop.
1How it is written
for value in values:
statement
if it is time to stop:
break
statement
next statement <- break jumps hereA single word, on a line of its own, inside the loop body — almost always as the block of an if, because a break with no condition stops the loop on its first round.
2Counting to 9, stopping at 5
# print 1 to 9 — but stop as soon as i reaches 5
for i in range(1, 10):
if i == 5:
break
print(i)1 2 3 4
The loop was set up for nine rounds and ran four. On the fifth, i == 5 was True, break ran, and that was that — print(i) never ran for 5, and 6, 7, 8 and 9 were never reached at all.
break comes before print(i), so on the round where i is 5 the program leaves the loop without ever getting to the print.3Move one line and the answer changes
Same loop, same condition, same break — with the print() moved above the if instead of below it:
# the print now comes first
for i in range(1, 10):
print(i)
if i == 5:
break1 2 3 4 5
Now 5 is printed, because the printing happened before the program reached the break. Statements in a body still run top to bottom; break only stops what comes after it.
break in it, always ask which lines come before the break and which come after. That is the whole difference between these two programs, and it is worth a mark.4Step through it
The tracer from the last lesson, with the pointer on break. Step to the round where i is 4 and watch the pointer leave the loop body altogether:
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.
5What break is really for: searching
This is the job break was made for. Once you have found what you were looking for, there is nothing to gain by looking at the rest:
# stop looking the moment the name turns up
names = ['Aarav', 'Diya', 'Kabir', 'Meera', 'Rohan']
wanted = 'Kabir'
for name in names:
print('Checking', name)
if name == wanted:
print('Found', wanted)
breakChecking Aarav Checking Diya Checking Kabir Found Kabir
Meera and Rohan were never checked. On a list of five that is a small saving; on a list of five lakh names it is the difference between a program that answers instantly and one you wait for.
6break with while True
You met while True as the loop that never ends on its own. With break it becomes something genuinely useful: a loop that runs until something inside it decides to stop, rather than until a condition at the top says so.
# keep printing hello until the user types bye
while True:
choice = input('Enter bye to exit, or anything else for hello: ')
if choice == 'bye':
break
print('hello')
print('Goodbye!')Enter bye to exit, or anything else for hello: hi hello Enter bye to exit, or anything else for hello: ok hello Enter bye to exit, or anything else for hello: bye Goodbye!
The condition True is always true, so the only way out is the break. This is exactly how a game keeps running until the player picks exit from the menu, and it is the standard shape for any “keep going until the user says stop” program.
while loop, forgetting to update the variable gives you an endless loop. In a while True loop, forgetting the break — or writing a condition that can never be True — does the same thing. Before you run one, find the line that ends it.Try it yourself. The values you type are fed to the program in order:
7In nested loops, break leaves only the inner one
This is the other place marks are lost. A break ends the loop it is written in — and when loops are nested, that is the inner one. The outer loop carries on as if nothing had happened:
# the break belongs to the inner loop only
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
break
print(i, j)1 1 2 1 3 1
The inner loop was cut short on all three of its runs — that is why j is never 2 or 3 in the output. But the outer loop still ran its full three rounds, so i went 1, 2, 3 as usual.
break escapes one loop: the innermost one it is inside. There is no way to break out of two loops at once in Python — a second break in the outer body would be needed for that.8Recap
break terminates the loop it is used in. The rest of the current round is skipped, no further values are handed out, and control goes to the first statement after the loop. It is a keyword, it must be inside a loop, it is normally the block of an if, and any statement after it in the same block is unreachable. In nested loops it leaves the inner loop only.A loop runs for i in range(1, 6). Its body checks if i == 3 and breaks, and the line after that check prints i. What is printed?
A break sits inside an inner loop, which sits inside an outer loop. What does it end?
Why is a break almost always written inside an if?