do ... while loop
skaller
skaller at maxtal.com.au
Sun Oct 17 10:50:33 EDT 1999
Joshua Marcus wrote:
>
> Okay, I'm terribly embarassed. My example was hasty and poorly chosen.
> I blame the heat of the moment.
>
> In my initial example, I wanted to assume that one wanted to run the 'process'
> function at least once-- which is why the initial line couldn't be
> "line = fp.readline()".
>
> But -- the "for line in fp.readlines():" is fine for many situations.
> Let's move away from this example.
>
> There are, in fact, other situations in which one actually wants a
> "do...while" loop & one is forced to either repeat code or make a meaningless
> initial assignment.
While this is true, just adding another hacked loop
construct isn't the answer. It will solve some problems,
and leave others. A more general solution is:
loop:
code ..
if ...: break
code ..
However, even that isn't general enough, because
many loops have multiple exits. For example,
a search loop: there is no clean way to program
this using C style loops.
So an even more general construction is:
loop:
..
breakto label1
..
breakto label2
label1: ...
label2: ...
This construction was invented by Knuth, except he put
the handlers at the top of the loop. A python
implementation is:
try:
while 1:
.. raise Label1
... raise Label1
except Label1: ..
except Label2:
--
John Skaller, mailto:skaller at maxtal.com.au
1/10 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
downloads: http://www.triode.net.au/~skaller
More information about the Python-list
mailing list