do ... while loop

Joshua Marcus josh at linc.cis.upenn.edu
Wed Oct 13 22:37:54 EDT 1999


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.  Sometimes one actually wants to run a block of code 
before the initial test.  I'm sure other folks use 'do...while's often enough
in other languages (say C) and I'd be surprised if they find the 'do...while'
loops confusing to read.

Because there are many situations in which one might be making an assignment
directly before a while loop, I haven't found an easy idiom for a 'do...while'
loop in python without a clearly marked meaningless assignment (hence the 
initial 'foo' assignment).

"Syntactic sugar" isn't a slur; while we don't theoretically need "syntactic 
sugar", the sweet-stuff makes code easier to read.  I'm only trying to agitate
for the the third of the three C loops here, not trying to open up the 
floodgates in the face of puritanism.

--j






More information about the Python-list mailing list