Lets continue

When Python sees a continue statement in the middle of a loop code block, it skips the rest of the code in the block and goes back to the top of the loop. Here's how it works. This code block prints both "5" and "five" because the print n, statement is executed each time through the loop.

>>> for n in range(10): ... if n == 5: ... print "five", ... print n,

When we add continue to the if block, Python skips the last print n, statement when the loop condition evaluates as true; therefore, it prints only "five":

>>> for n in range(10): ... if n == 5: ... print "five", ... continue ... print n,

Tip Use continue statements sparingly because they can make your code hard to read, especially if they are nested in complex structures.

Was this article helpful?

0 0

Post a comment