do while loop

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Apr 26 18:54:05 EDT 2006


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




More information about the Python-list mailing list