do while loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • é›·

    #1

    do while loop

    suggest add do while loop in later version

  • Marc 'BlackJack' Rintsch

    #2
    Re: do while loop

    In <1146012542.166 096.78690@g10g2 000cwb.googlegr oups.com>, é›· wrote:
    [color=blue]
    > suggest add do while loop in later version[/color]

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

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Rick Zantow

      #3
      Re: do while loop

      Marc 'BlackJack' Rintsch <bj_666@gmx.net > wrote in
      news:pan.2006.0 4.26.20.41.03.1 89115@gmx.net:
      [color=blue]
      > In <1146012542.166 096.78690@g10g2 000cwb.googlegr oups.com>, é›· wrote:
      >[color=green]
      >> suggest add do while loop in later version[/color]
      >
      > Please also suggest a clean syntax for this. :-)
      >[/color]

      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

      Comment

      • Dave Hansen

        #4
        Re: do while loop

        On Wed, 26 Apr 2006 22:41:04 +0200 in comp.lang.pytho n, Marc
        'BlackJack' Rintsch <bj_666@gmx.net > wrote:
        [color=blue]
        >In <1146012542.166 096.78690@g10g2 000cwb.googlegr oups.com>, ? wrote:
        >[color=green]
        >> suggest add do while loop in later version[/color]
        >
        >Please also suggest a clean syntax for this. :-)
        >[/color]

        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.

        Comment

        • Thomas Nelson

          #5
          Re: do while loop

          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

          Comment

          • bearophileHUGS@lycos.com

            #6
            Re: do while loop

            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

            Comment

            Working...