Python loop constructs

shawnwheatley at mailcan.com shawnwheatley at mailcan.com
Wed Jan 15 14:43:13 EST 2003


On 14 Jan 2003 20:08:04 -0800, "Jonathan P." <jbperez808 at yahoo.com> said:
> But doesn't the present 'while' syntax already give
> you all that and more?
> 
> while 1:
>   do-stuff
>   if condition: break
> 
> while condition:
>   do-stuff
> 
> If one thinks of while 1: as an alias for loop: then
> I don't find anything objectionable to its usage at all.
> In addition:
> 
>   while (condition): do-stuff
> 
> would be a lot clearer than:
> 
>   loop (condition): do-stuff
> 
> or
> 
>   loop while (condition): do-stuff # YUCK!

Keep in mind, however, that:
while(condition):
    do stuff

and something like:
while 1:
    do stuff
    if condition, break

are two very different loops.  The first is a (0..n) loop, or a loop
where the loop logic may not occur at all.  The second type (I've seen it
called a Do...While loop before) is a (1..n) loop, which ensures that the
loop action will occur at least once, no matter what the condition is.  I
can't think of any off the top of my head, but there are situations where
you want one behaviour or the other.

As to the question "Does Python need this loop construct?"--it doesn't
really matter to me.  While additional syntax may make the code more
readable, I think *most* people understand what the loop is trying to
accomplish.  If you think it may be vague, you can always add a comment.

Shawn
-- 
  
  shawnwheatley at mailcan.com

-- 
http://fastmail.fm - Sent 0.000002 seconds ago





More information about the Python-list mailing list