"also" to balance "else" ?

John Roth newsgroups at jhrothjr.com
Mon Jun 13 23:01:59 EDT 2005


"Ron Adam" <rrr at ronadam.com> wrote in message 
news:H6pre.131217$IO.122689 at tornado.tampabay.rr.com...

> Currently the else block in a for loop gets executed if the loop is 
> completed, which seems backwards to me.  I would expect the else to 
> complete if the loop was broken out of.  That seems more constant with 
> if's else block executing when if's condition is false.

Actually, it makes sense if you look at it correctly.

In an unadorned loop, exits via break and via the
loop condition becoming false go to the same place.
To distinguish requires some kind of a switch.

In a loop with an else, exits via break skip the else
clause, while an exit via the loop condition takes
the else clause. You don't need a special exit on
break since you can put any amount of logic after
the if and in front of the break. Where you need it
is on exit via the loop condition.

The difficulty you're having with this is that else
is a very bad keyword for this particular construct.
I'd prefer something like "on normal exit" as
a keyword.

John Roth 




More information about the Python-list mailing list