Is there a good reason why there isn't a do while flow control statement in python?
Why do pepole have to write while and break explicitly?
views:
157answers:
4
+10
A:
Probably because Guido didn't think it was necessary. There are a bunch of different flow-control statements you could support, but most of them are variants of each other. Frankly, I've found the do-while statement to be one of the less useful ones.
JesperE
2010-02-03 13:56:20
+1, i almost always want my conditions at the top. makes the 'zero case' a lot cleaner.
Javier
2010-02-03 14:00:07
Yes, probably GuiDO didn't think it worthWHILE
jellybean
2010-02-03 14:01:42
in Pascal I always use repeat-until(condition). In C++ I use do-while(condition) rarely. Can you explain that? :)
Nick D
2010-02-03 14:03:59
`while` itself is pretty useless when you have generators. Haven't used it in years for anything other than `while True`.
THC4k
2010-02-03 14:12:21
@Nick: I used repeat-until a lot when I did Pascal also. However, that was in the early-mid 1980's when I was in college. Usually the programs were text-based, menu-driven, and interactive, so the menu always had to appear at least once. A repeat-until made sense then. Now, most of my programming is file driven, so a while makes more sense.
GreenMatt
2010-02-03 14:29:03
I learned from EWD's *A Discipline of Programming*, so I can only use `while` and have only used `while` (and `for`). I saw other loop constructs, but couldn't figure out why there had to be so many syntax alternatives when `while` (and `for`) had the easiest proof rules.
S.Lott
2010-02-03 14:39:46
+5
A:
It has been proposed in PEP 315 but hasn't been implemented because nobody has come up with a syntax that's clearer than the while True with an inner if-break.
Pär Wieslander
2010-02-03 14:01:31
Have a look at http://mail.python.org/pipermail/python-dev/2006-February/060718.html which is linked from the PEP. It explains the problem with various syntax alternatives in a bit more detail.
Pär Wieslander
2010-02-03 14:13:19
+2
A:
Python adds features only when they significantly simplify some code.
while True:
...
if not cond: break
is not less simple than a do-while loop, for which there is no obvious natural python syntax anyway.
do:
...
while cond
(Looks weird)
or this?
do:
...
while cond
(The while looks like a regular while statement)
Paul Hankin
2010-02-03 14:17:25
A do-while/repeat-until loop is not the same thing as a while loop. The former will always execute at least once, but the latter may not execute at all.
GreenMatt
2010-02-03 22:13:06
@GreenMatt, that's not what he's talking about. You'll then have 2 ways to manually loop on a condition. Pre and post conditions don't _really_ differ enough to justify extending the Python syntax.
Matt Joiner
2010-02-03 22:22:24
@Matt Joiner: Firstly, I don't presume to know what Austin was thinking when he made that entry. To address your comment: I realize that the thinking is that Python doesn't need the extra looping structure. I'm just pointing out the difference lest someone who doesn't know otherwise would think the two looping mechanisms do exactly the same thing.
GreenMatt
2010-02-03 22:51:02
@GreenMatt, fair enough then. We're all friends here, long live Guido.
Matt Joiner
2010-02-04 00:30:06