469,915 Members | 2,697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,915 developers. It's quick & easy.

do while loop

suggest add do while loop in later version

Apr 26 '06 #1
5 11958
In <11*********************@g10g2000cwb.googlegroups. com>, 雷 wrote:
suggest add do while loop in later version


Please also suggest a clean syntax for this. :-)

Ciao,
Marc 'BlackJack' Rintsch
Apr 26 '06 #2
Marc 'BlackJack' Rintsch <bj****@gmx.net> wrote in
news:pa****************************@gmx.net:
In <11*********************@g10g2000cwb.googlegroups. com>, 雷 wrote:
suggest add do while loop in later version


Please also suggest a clean syntax for this. :-)


Since Python already has a while <condition>: loop, I'm supposing you
mean something like
do:
<some code>
while <not condition>
or
do:
<some code>
until <condition>

Is that what you mean? In some languages that might be a "do x until y"
loop, which I would suppose has been proposed before for Python. A
clean syntax might be

until <condition>:
<some code>

.... except that people seem to object to placing the condition
specification so far from where the test would actually take place (at
the bottom of the loop). I don't have a problem with that, but I can see
why many would. It doesn't seem Pythonic somehow.

There was a long thread about a loop-and-a-half construction where some
of this was discussed, though I don't have a link for that.

In any case, what would you want to do that you can't do (in some way)
now? If you have a compelling use case, you could get a bit more
attention than merely offering a suggested expansion of the language.

--
rzed
Apr 26 '06 #3
On Wed, 26 Apr 2006 22:41:04 +0200 in comp.lang.python, Marc
'BlackJack' Rintsch <bj****@gmx.net> wrote:
In <11*********************@g10g2000cwb.googlegroups. com>, ? wrote:
suggest add do while loop in later version


Please also suggest a clean syntax for this. :-)


while 1:
do_loop_stuff()
if time_to_leave(): break

Well, maybe not too clean, but it works today...

For the future, maybe:

do: #or "repeat:"
do_loop_stuff()
until time_to_leave()

Regards,
-=Dave

--
Change is inevitable, progress is not.
Apr 26 '06 #4
My usual way of emulating do-while is:

started = False
while (someBoolean or not started):
started = True
#whatever else

This simply assures "whatever else" happens at least once. Is this
Pythonic?

THN

Apr 26 '06 #5
Rick Zantow>In any case, what would you want to do that you can't do
(in some way) now?<

You can do all things already, that's not the point, I presume. Some
things added in the last years were already possibile in different
ways.
Rick Zantow>If you have a compelling use case,<

I agree that such use cases aren't much common, but I have found 2-3 of
them every 6 months (but I was used with a Pascal-like language, where
repeat-until is used often). I usually solve the problem with something
like:

while True:
... # things to do
if condition:
break

That isn't nice, looks like coming from an old languge, but works.
Dave Hansen:

do: #or "repeat:"
do_loop_stuff()
until time_to_leave()

I was used with Pascal (that uses repeat-until) but I think the version
with do-while is better, you don't have to remember to invert the
condition. I like the do-while version without the else. To me this
seems a good simple syntax:

do:
val = source.read(1)
process(val)
while val != lastitem

This is from PEP 315, I don't like it, but it's more general:

do:
<setup code>
while <condition>:
<loop body>

I presume you end doing a lot of:

do:
# code
while <condition>:
pass

Is the loop body used often in this situation?

Bye,
bearophile

Apr 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Charles Alexander | last post: by
43 posts views Thread by Gremlin | last post: by
5 posts views Thread by Martin Schou | last post: by
32 posts views Thread by Toby Newman | last post: by
2 posts views Thread by Alex | last post: by
3 posts views Thread by Ben R. | last post: by
32 posts views Thread by cj | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.