while (a=b()) ...

Dan Schmidt dfan at harmonixmusic.com
Wed May 12 09:56:38 EDT 1999


Jim Meier <fatjim at home.com> writes:

| [Dan Schmidt wrote]
| > Your loop would look something like this:
| >
| >   while 1 until done:
| >     value = getValue()
| >     if not value: done
| >     process(value)
| >
| > 'done' is not a keyword; it's basically an exception (the name of
| > which was generated by the 'until' clause) that kicks us out of the
| > loop.
| 
| Just to stick in my own 2cents here, an idiom i find extremely readable
| is this:
| 
| done=false
| while not done:
|     (do stuff)
|     if test:
|         done=true
| 
| Which seems very similar but without magic language changes.

That doesn't really accomplish the same thing.

The point that the debate is about is the need to break out of the
loop between getting a value and using it.

This code:

  done=false
  while not done:
      value = getValue()
      if test(value):
          done=true
      process(value)

doesn't help because setting done to true doesn't exit the loop until
we come around again to the while.

I guess you could write

  done=false
  while not done:
      value = getValue()
      if test(value):
          done=true
      else:
          process(value)

but, ugh.

-- 
                 Dan Schmidt -> dfan at harmonixmusic.com, dfan at alum.mit.edu
Honest Bob & the                http://www2.thecia.net/users/dfan/
Factory-to-Dealer Incentives -> http://www2.thecia.net/users/dfan/hbob/
          Gamelan Galak Tika -> http://web.mit.edu/galak-tika/www/




More information about the Python-list mailing list