else clauses in while and for loops

Jeff Massung jmassung at magpiesystems.com
Mon Apr 17 22:50:19 CEST 2000


I'm new to Python, so perhaps I'm miss reading something in these posts, but
it appears as though the "else" statement (especially in while loops -
unless infinite) always get executed regardless.

i = 0
while i<=5:
    i = i + 1
    print i
else:
    print 10

should spit out "1 2 3 4 5 10" right?

Now a use that I could definitely see for the "else" would be if the loop
were never entered at all! Example:

i = 6
while i<=5:
    i = i+1
    print i
else:
    print 10

would print "10", and if 'i' started at 0, would print "1 2 3 4 5".

That could definitely be useful, because it would eliminate an "if"
statement right before the "while" statement. Extremely useful for debugging
purposes.

Jeff

Chuck Esterbrook wrote in message <38F79B93.B03F0A6B at mindspring.com>...
>So did anyone present, in this thread, an example that shows a real need?






More information about the Python-list mailing list