(original) (raw)

The way I remember it is to observe that the following are \*almost\* exactly the same thing:

if C:
T
else:
E

while C:
T
else:
E

The \*only\* differences are:

1) where execution jumps if it reaches the end of the T: in the "while", it jumps back to the while itself, resulting in the condition being rechecked, whereas in the "if" execution skips over the "else" to whatever follows; and

2) in the "while", inside the T "break" and "continue" relate to this control structure rather than to a containing loop.

(At least I don't think I'm missing any other differences!)

Seen this way, the meaning of the "else" is easy to understand and to remember.

And the for loop else is like the while loop else.


On 24 July 2017 at 11:35, Kiuhnm via Python-Dev <python-dev@python.org> wrote:
Hello,

I think that the expression "for...else" or "while...else" is completely counter-intuitive. Wouldn't it be possible to make it clearer? Maybe something like

break in for i in range(n):
...
if cond:
break
else:
...

I'm not an English native speaker so I don't know whether "break in" is acceptable English in this context or can only mean "to get into a building by force".

Kiuhnm
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/isaac.morland%40gmail.com