7

I'm confused about the use of the continue statement in a while loop.

In this highly upvoted answer, continue is used inside a while loop to indicate that the execution should continue (obviously). It's definition also mentions its use in a while loop:

continue may only occur syntactically nested in a for or while loop

But in this (also highly upvoted) question about the use of continue, all examples are given using a for loop.

It would also appear, given the tests I've run, that it is completely unnecessary. This code:

while True:
    data = raw_input("Enter string in all caps: ")
    if not data.isupper():
        print("Try again.")
        continue
    else:
        break

works just as good as this one:

while True:
    data = raw_input("Enter string in all caps: ")
    if not data.isupper():
        print("Try again.")
    else:
        break

What am I missing?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Gabriel
  • 40,504
  • 73
  • 230
  • 404
  • 2
    You're not missing anything - in your simple example, it's completely unnecessary. In a much more complicated example (or in more poorly organized code) it can be useful (for example, you can remove the "else" clause if you keep the continue in there). – Fhaab Jul 21 '16 at 20:27
  • 1
    Both examples have the same behaviour: the `continue` is programmatically useless here. In your examples, it is useful for a reviewer to understand more easily you algorithm. It can be useful too for IDEs to automatically unindent the next line like with the `pass` statement. – Frodon Jul 21 '16 at 20:28
  • 2
    `continue` means: Terminate *this iteration* of the loop (i.e. *skip* the rest of the loop body) and *continue* with the next iteration. – Lukas Graf Jul 21 '16 at 20:31
  • The answers to the [question you linked](http://stackoverflow.com/questions/8420705/example-use-of-continue-statement-in-python) already explain the point of `continue`. That their examples use `for` instead of `while` doesn't matter at all. `continue` is the same for both kinds of loops. – xgord Jul 21 '16 at 20:34

3 Answers3

11

Here's a really simple example where continue actually does something measureable:

animals = ['dog', 'cat', 'pig', 'horse', 'cow']
while animals:
    a = animals.pop()
    if a == 'dog':
        continue
    elif a == 'horse':
        break
    print(a)

You'll notice that if you run this, you won't see dog printed. That's because when python sees continue, it skips the rest of the while suite and starts over from the top.

You won't see 'horse' or 'cow' either because when 'horse' is seen, we encounter the break which exits the while suite entirely.

With all that said, I'll just say that over 90%1 of loops won't need a continue statement.

1This is complete guess, I don't have any real data to support this claim :)

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 'continue' could also be avoided by this way animals = ['dog', 'cat', 'pig', 'horse', 'cow'] while animals: a = animals.pop() if a == 'horse': break if a != 'dog': print(a) – Jcyrss Feb 16 '17 at 13:10
2

continue just means skip to the next iteration of the loop. The behavior here is the same because nothing further happens after the continue statement anyways.

The docs you quoted are just saying that you can only use continue inside of a loop structure - outside, it's meaningless.

Athena
  • 3,200
  • 3
  • 27
  • 35
  • Ahh so `continue` breaks *inside* the loop, while `break` breaks *outside* of it. Did I understand you correctly? – Gabriel Jul 21 '16 at 20:27
  • 2
    Yeah, that's a way of looking at it. I frequently use `continue` as a way of saying 'discard the rest of this loop body, we aren't interested in it anymore. But don't exit the loop, just discard this one step in iteration.' – Athena Jul 21 '16 at 20:30
2

continue is only necessary if you want to jump to the next iteration of a loop without doing the rest of the loop. It has no effect if it's the last statement to be run.

break exits the loop altogether.

An example:

items = [1, 2, 3, 4, 5]
print('before loop')
for item in items:
    if item == 5:
        break
    if item < 3:
        continue
    print(item)

print('after loop')

result:

before loop
3
4
after loop
MaxNoe
  • 14,470
  • 3
  • 41
  • 46