Break and continue are two ways to modify the behavior of for loops and while loops. In Python, the keyword An example using In [1]:Break and Continue
Break
break
causes the program to exit a loop
early. break
causes the program to jump out of for loops even if the for loop hasn't run the specified number of times.break
causes the program to jump out of while loops even if the logical condition that defines the loop is still True
.break
in a for loop is below. for i in range[100]:
print[i]
if i == 3:
break
print['Loop exited']
i=3
, break
is encountered and the program exits the loop. An example using break
in a while loop is below.
In [2]:
while True: out = input['type q to exit the loop: '] if out == 'q': break print['Loop exited']
type q to exit the loop: no type q to exit the loop: q Loop exited
Continue
In Python, the keyword continue
causes the program to stop running code in a loop and start back at the top of the loop. Remember the keyword break
cause the program to exit a loop. continue
is similar, but continue
causes the program to stop the current iteration of the loop and start the next iteration at the top of the loop.
A code section that
uses continue
in a for loop is below.
In [3]:
for i in range[4]: if i==2: continue print[i]When the code section is run, the number
2
is not printed. This is because when i=2
the program hits the continue
statement. Therefore, the line print[i]
isn't run when i=2
. Then the program starts back up at the start of the loop with the next number i=3
. How do I leave a loop early in python?
for a in b:
if criteria in list1:
print "oh no"
#Force loop i.e. force next iteration without going on
someList.append[a]
Also, in java you can break
out of a loop, is there an equivalent in Python?
matt wilkie
16.1k22 gold badges75 silver badges109 bronze badges
asked Feb 2, 2010 at 13:23
1
continue
and break
is what you want. Python works identically to Java/C++ in this regard.
answered Feb 2, 2010 at 13:25
Max ShawabkehMax Shawabkeh
36.8k9 gold badges80 silver badges91 bronze badges
1
Firstly, bear in mind it might be possible to do what you want with a list comprehension. So you might be able to use something like:
somelist = [a for a in b if not a.criteria in otherlist]
If you want to leave a loop early in Python you can use break
, just like in Java.
>>> for x in xrange[1,6]:
... print x
... if x == 2:
... break
...
1
2
If you want to start
the next iteration of the loop early you use continue
, again just as you would in Java.
>>> for x in xrange[1,6]:
... if x == 2:
... continue
... print x
...
1
3
4
5
Here's the documentation for break
and continue
. This also covers else
clauses for loops, which aren't run when you break
.
answered Feb 2, 2010 at 13:25
David WebbDavid Webb
187k57 gold badges308 silver badges298 bronze badges
continue
and break
work exactly like in other programming languages,
except that you cannot break
to a label [as you can in Java, for example]. That means you can only break one loop at a time.
answered Feb 2, 2010 at 13:28
AndiDogAndiDog
66.7k20 gold badges157 silver badges201 bronze badges
Take a look at break
and continue
.
answered Feb 2, 2010 at 13:25
YacobyYacoby
53.3k13 gold badges111 silver badges119 bronze badges