Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Understanding while...else...

Thread Tools

Re: Understanding while...else...

Terry Reedy
Posts: n/a
On 1/22/2013 3:09 PM, Ethan Furman wrote:
> On 01/22/2013 09:44 AM, Terry Reedy wrote:
>> Several people have trouble understanding Python's while-else and
>> for-else constructs. It is actually quite simple if one starts with
>> if-else, which few have any trouble with.
>> Start with, for example
>> if n > 0:
>> n -= 1
>> else:
>> n = None
>> The else clause is executed if and when the condition is false. (That
>> the code is useless is not the point here.) Now use pseudo-Python label
>> and goto statements to repeatedly decrement n
>> label: check
>> if n > 0:
>> n -= 1
>> goto: check
>> else:
>> n = None
>> The else clause is executed if and when the condition is false.
>> Now use a real Python while statement to do the *same
>> thing*.
>> while n > 0:
>> n -= 1
>> else:
>> n = None

> I understand how it works (although it did take a while for it to sink
> in); my gripe, and probably why it is misunderstood so often, is that
> nine times out of ten when I /want/ to use a while-else or for-else I
> only want the true/false check /once/, at the beginning of the loop.

I do not understand what you are saying. There already is only one
true/false check, at the beginning of the loop. If you only want the
check *performed* once, you would use if-else. But I presume you know this.

Terry Jan Reedy

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding your BIOS TYCOON Hardware 6 06-29-2005 09:54 AM
Confirm my wireless understanding please? Evil Uncle Chris Wireless Networking 1 05-01-2005 03:19 PM
Understanding voice AIMs Ghazan Haider Cisco 1 11-28-2004 03:15 PM
Re: understanding an error Alvin Andries VHDL 0 09-12-2003 11:38 AM
Why does Microsoft have such a hard time understanding what they say? George Hester ASP .Net 3 08-11-2003 09:16 PM