Friday Finking: Contorted loops

Terry Reedy tjreedy at udel.edu
Fri Sep 10 21:30:32 EDT 2021


On 9/10/2021 7:38 AM, Alan Gauld via Python-list wrote:

> But python complicates this tenet still further by adding an else
> clause to its loops. And complicating this still more is that these
> else clauses have almost exactly opposite effects.

To the contrary...

if...else
executes the else part if the condition is false.

> 
> while...else...
> 
> executes the else if the body of the loop does NOT get executed.

IE, executes the else part if the condition is false.
A while statement is, or can be viewed as, an if statement with a goto 
ending the if part.


> for...else...
> 
> executes the else iff ALL iterations of the for loop DO complete.

IE, executes the else part of the condition is false.
A for loop is, or can be viewed as syntactic sugar for a while loop. 
The condition is that next(iterable) yields a value.

It is possible that the doc could be improved.  I have not looked for a 
while.  Or maybe it needs to be read more.

-- 
Terry Jan Reedy



More information about the Python-list mailing list