1

I'm not sure how the continue statement is interpreted when it is inside a for loop with an else clause.

If the condition is true, the break will exit from a for loop and else part will not be executed. And if the condition is False then else part will be executed.

But, what about continue statement? I tested it seems that the after the continue statement is reached, the else part will be executed. Is this true?? Here is a code example:

# when condition found and it's `true` then `else` part is executing :

edibles = ["ham", "spam", "eggs","nuts"]
for food in edibles:
    if food == "spam":
        print("No more spam please!")
        continue
    print("Great, delicious " + food)

else:
    print("I am so glad: No spam!")
print("Finally, I finished stuffing myself")`

If I remove "spam" from the list, now the condition is always false and never found but still the else part is executed:

edibles = ["ham","eggs","nuts"]
for food in edibles:
    if food == "spam":
        print("No more spam please!")
        continue
    print("Great, delicious " + food)

else:
    print("I am so glad: No spam!")
print("Finally, I finished stuffing myself")
James K
  • 3,692
  • 1
  • 28
  • 36
  • `for/else` use only `break` and `else` doesn't care for `continue` - it is `for-break-else` construction. (see @Charles answer) – furas Oct 02 '16 at 09:46

2 Answers2

5

With a for loop in Python, the else block is executed when the loop finishes normally, i.e. there is no break statement. A continue does not affect it either way.

If the for loop ends because of a break statement, then else block will not execute. If the loop exits normally (no break), then the else block will be executed.

From the docs:

When used with a loop, the else clause has more in common with the else clause of a try statement than it does that of if statements: a try statement’s else clause runs when no exception occurs, and a loop’s else clause runs when no break occurs.

I always remember it because of how Raymond Hettinger describes it. He said it should have been called nobreak instead of else. (That's also a good video that explains the usefulness of the for-else construct)

Example:

numbers = [1,2,3]
for number in numbers:
    if number == 4:
        print("4 was found")
        break
else:
    print("4 was not found")

When you run the above code, since 4 is not in the list, the loop will not break and the else clause will print. If you add 4 to the list and run it again, it will break and the else will not print. In most other languages, you would have to add some sentinel boolean like found and make it True if you find a 4, then only print the statement after the loop if found is False.

charlie
  • 856
  • 1
  • 11
  • 17
  • 1
    And of course if the `for` (or `while`) loop is in a function and you exit the loop prematurely using `return` then the `else` clause won't be executed either. – PM 2Ring Oct 02 '16 at 10:10
2

Your else part will be executed in both cases. else part executed when loop terminate when condition didn't found.Which is what is happening in your code. But it will also work same without continue statement.

now what about break statement's else part, Break statement's else part will be executed only if:

  • If the loop completes normally without any break.
  • If the loop doesn't encounter a break.

enter image description here

Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88
  • Thanks :) i like your explanation can you please answer this question ? http://stackoverflow.com/questions/40006300/how-two-recursion-function-in-program-works#40006300 –  Oct 13 '16 at 09:57