7. Continue Statement (Keyword)

Although the break statement comes in handy, the continue statement is just as useful. Unlike the break statement, the continue statement will stop the current execution of code and go back to the beginning of the while statement. This can be used to skip part of the code and not end the loop at the same time. The continue statement is used just like the break statement. An example is given below.

>>> bacon = 0
>>> while bacon != 10:
...     bacon += 1
...     if bacon < 5:
...         continue
...     print(bacon)
...
5
6
7
8
9
10


Although the example could have easily omitted the continue, it demonstrates how it works. The continue statement is used to reduce the amount of indenting and nesting used; it would be easier to check five conditions and use a continue if they fail, than to nest five if statements. This can reduce the amount of typing needed and, as a result, will increase readability and code clarity.

"The Continue Statement" by wikiversity is licensed under CC BY-SA 4.0