Friday Finking: Contorted loops

jak nospam at please.ty
Sun Sep 12 04:11:15 EDT 2021


-- snip --
> 
> An inconsistency that I have been able to notice is this:
> someone suggests to remedy the absence of the do-while with:
> while True:
>       ...
>       if condition:
>           break
> the problem arises if the while has an else of its own because the break
> not only blocks the while loop but will also ignore the relative else.
> 

I will try to make my doubt clearer:
if the only way to terminate a 'while True' loop is by using the 'break' 
statement, why is it allowed to add the 'else' statement which will only 
contain dead code?

while True:
     break
else:
     print('dead code')


More information about the Python-list mailing list