PEP 315: Enhanced While Loop

W Isaac Carroll icarroll at pobox.com
Mon May 5 15:01:54 EDT 2003


Using the following syntax as the example:

     while <condition1>:
         <code1>
     and while <condition2>:
         <code2>
     and while <condition3>:
         <code3>

My preference is to define the loop as all of the code blocks together, 
and treat all the while clauses symmetrically. My original proposal only 
allowed one while clause, but I see no particular reason to keep to that 
limit. Any CS people know of a reason why multiple loop conditions would 
be a bad thing?

andrew cooke wrote:
> - if condition1 is true but condition2 is false is code1 repeated?

No. The loop will exit after finding condition2 to be false.

> - what if condition2 is true but condition1 is false?

The loop will exit after finding condition1 to be false.

> - is there any meaning for "or while"?

The use of "and" is motivated by a desire to not introduce new keywords 
into the language. There is no proposed meaning for "or while".

> - if condition1 and condition2 are both true do both code1 and code2 get
> repeated together, or is nesting implied (does code2 repeat until
> condition2 is false then you start with code1 again?)?

No nesting is implied. The loop repeats as long as it finds all 
conditions to be true when they are evaluated. The first one found to be 
false causes the entire loop to exit, transferring control to the else 
clause, if present.

> these aren't necessaril criticisms - maybe i'd ask similar things about
> existing constructs if they were new to me.  but it doesn't seem totally
> obvious what the answers are (i'm guessing the answers are: yes, nothing,
> no, together, but i'm not 100% sure on any of them)

Fair enough. Since this construct is not a typical one found in other 
languages, no one is expected to immediately know what it does at first 
glance. However, I believe that the "and while" syntax is easy enough to 
recognize and remember that once people learn its semantics there won't 
be any confusion.

TTFN






More information about the Python-list mailing list