do while loop

R

Rick Zantow

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

Dave Hansen

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
 
T

Thomas Nelson

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
 
B

bearophileHUGS

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
 
D

Dennis Lee Bieber

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?
Knowing Python, I suspect one would more often have

do:
pass
while not bored:
bored = thumbs.twiddle()

UNLESS (and I've not read the documents) this is meant to be a synonym
for:

while True:
data = something()
if data == "EndIt": break
mangle(data)
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top