How else part work in continue statement?
<p>I'm not sure how the <code>continue</code> statement is interpreted when it is inside a <code>for</code> loop with an <code>else</code> clause.</p>

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

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

<pre><code># 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")`
</code></pre>

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

<pre><code>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")
</code></pre>

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

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

<p>From the <a href="https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops" rel="nofollow">docs</a>:</p>

<blockquote>
  <p>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.</p>
</blockquote>

<p>I always remember it because of how Raymond Hettinger <a href="https://youtu.be/OSGv2VnC0go?t=17m44s" rel="nofollow">describes it</a>. He said it should have been called <code>nobreak</code> instead of <code>else</code>. (That's also a good video that explains the usefulness of the for-else construct)</p>

<p>Example:</p>

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

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

python for-loop for-else