while (a=b()) ...

Dan Schmidt dfan at harmonixmusic.com
Tue May 11 17:29:07 EDT 1999


"Evan Simpson" <evan at tokenexchange.com> writes:

| This has been suggested (and Guido has said that he might well put it in) as
| "and while", so that loop starts can be distinguished from loop
| continuations.  Thus:
| 
| while 1:
|   value = getValue()
| and while value:
|   process(value)
| 
| Moshe Zadka wrote:
| > do:
| >   line=sys.stdin.readline()
| > while line:
| >   sys.stdout.write(line)

Again, I'm going to recommend Knuth's "Structured Programming with go
to statements", which contains 70 pages of discussion of this sort of
thing, found in his book "Literate Programming".

The method he advocates is basically dynamically generated exception
names (though he doesn't call them that).

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.

Well, that was easy enough to do with just a break.  Here's a little
more complicated example:

  for i in range(len(array)) until found:
    if array[i] == val: found(i)
  when found(index):
    print 'I found it at index', `index`
  else:
    print 'My search was fruitless'

There are more interesting examples in the article.

-- 
                 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