3 ms·
> what does "else: do after a while loop in python? Only people who know python know what it does (and I suspect most don't). OK, I had never heard of the synt
by thaumasiotes 11mo ago
> what does "else: do after a while loop in python? Only people who know python know what it does (and I suspect most don't).
OK, I had never heard of the syntax, but in its own defense it does exactly what you'd guess, the same thing it does after an "if".
These are equivalent statements:
preloop:
if condition:
do_more_stuff()
goto preloop
while condition:
do_more_stuff()
and these are also equivalent:
preloop:
if condition:
do_more_stuff()
goto preloop
else:
wrap_it_ip()
while condition:
do_more_stuff()
else:
wrap_it_up()
- martinflack 11mo agoIt could've easily been defined that the else branch runs if the while condition never had a true value at all. In fact, I think that's more intuitive.
- thaumasiotes 11mo agoWhat are you trying to say? It is defined that way. And the example I provided above makes that completely explicit. But here, from the official documentation: > if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates. https://docs.python.org/3/reference/compound_stmts.html#the-while-statement https://docs.python.org/3/reference/compound_stmts.html#the-...