Loop until condition is true

Discussion in 'Python' started by Remi Villatel, Jun 18, 2005.

  1. Hi there,

    There is always a "nice" way to do things in Python but this time I can't
    find one.

    What I'm trying to achieve is a conditionnal loop of which the condition
    test would be done at the end so the loop is executed at least once. It's
    some way the opposite of "while".

    So far, all I got is:

    while True:
    some(code)
    if final_condition is True:
    break
    #
    #

    What I don't find so "nice" is to have to build an infinite loop only to
    break it.

    Is there a better recipe?

    --
    ==================
    Remi Villatel
    maxilys_@_tele2.fr
    ==================
    Remi Villatel, Jun 18, 2005
    #1
    1. Advertising

  2. Remi Villatel

    Donn Cave Guest

    Quoth Remi Villatel <maxilys@SPAMCOP_tele2.fr>:

    | What I'm trying to achieve is a conditionnal loop of which the condition
    | test would be done at the end so the loop is executed at least once. It's
    | some way the opposite of "while".
    |
    | So far, all I got is:
    |
    | while True:
    | some(code)
    | if final_condition is True:
    | break
    | #
    | #
    |
    | What I don't find so "nice" is to have to build an infinite loop only to
    | break it.
    |
    | Is there a better recipe?

    It depends, but that isn't too bad. The alternative would be flag
    variable, like "looping = 1; while looping: ..."

    The construct you're looking for is "until" in C, and there have been
    plenty of proposals to add this or other improvements to Python's
    repertoire. As far as I know, it hasn't happened because it isn't
    really needed. If you look at C code, at least in my experience the
    "until" loop is quite rarely used. (I don't see it once in the source
    to Python 2.4, for example.) Meanwhile, the "while True" (or "while 1")
    idiom is very familiar to Python programmers (just as the Python source
    has dozens of "for (;;)"), so it's preferred for legibility. It's nice,
    don't worry.

    Donn Cave,
    Donn Cave, Jun 18, 2005
    #2
    1. Advertising

  3. Remi Villatel

    Peter Otten Guest

    Donn Cave wrote:

    > If you look at C code, at least in my experience the
    > "until" loop is quite rarely used.  (I don't see it once in the source
    > to Python 2.4, for example.)


    Long time no C?

    'until' in C is actually

    do
    statement
    while (expression);

    I found 136 occurrences of "do {" versus 754 of "while (" and 1224 of "for
    (" in the Python 2.4 source, so using these rough estimates do-while still
    qualifies as "rarely used".

    Peter
    Peter Otten, Jun 18, 2005
    #3
  4. On 2005-06-18, Peter Otten <> wrote:

    >> If you look at C code, at least in my experience the
    >> "until" loop is quite rarely used.  (I don't see it once in the source
    >> to Python 2.4, for example.)

    >
    > Long time no C?
    >
    > 'until' in C is actually
    >
    > do
    > statement
    > while (expression);
    >
    > I found 136 occurrences of "do {" versus 754 of "while (" and 1224 of "for
    > (" in the Python 2.4 source, so using these rough estimates do-while still
    > qualifies as "rarely used".


    AFAICT, the main use for do/while in C is when you want to
    define a block of code with local variables as a macro:

    #define DoSomething(foo) \
    do \
    { \
    int i; \
    /* do something with foo and i */ \
    } while (0)

    --
    Grant Edwards grante Yow! I HIJACKED a 747 to
    at get here!! I hope those
    visi.com fabulous CONEHEADS are
    at HOME!!
    Grant Edwards, Jun 18, 2005
    #4
  5. Remi Villatel

    Tim Williams Guest

    ----- Original Message -----
    From: "Remi Villatel" <maxilys@SPAMCOP_tele2.fr>


    > There is always a "nice" way to do things in Python but this time I can't
    > find one.


    > So far, all I got is:
    >
    > while True:
    > some(code)
    > if final_condition is True:
    > break
    > #
    > #
    >
    > What I don't find so "nice" is to have to build an infinite loop only to
    > break it.


    If your final_condition is already defined outside the "while" then how
    about

    while not final_condition:
    some(code)

    Simplistically, this equates to

    >>> x = 0
    >>> while not x == 5:

    .... x += 1
    ....
    >>> print x

    5

    :)
    Tim Williams, Jun 18, 2005
    #5
  6. Remi Villatel

    Donn Cave Guest

    Quoth Peter Otten <>:
    ....
    | 'until' in C is actually
    |
    | do
    | statement
    | while (expression);

    Oops. Well, QED - I sure don't need it often.

    Donn Cave,
    Donn Cave, Jun 18, 2005
    #6
  7. On Sat, 18 Jun 2005 13:35:16 -0000, Grant Edwards <>
    wrote:

    >AFAICT, the main use for do/while in C is when you want to
    >define a block of code with local variables as a macro:


    When my job was squeezing most out of the CPU (videogame
    industry) I remember that the asm code generated by

    while (sz-- > 0)
    {
    /* do some stuff */
    }

    was indeed worse than

    do
    {
    /* do some stuff */
    } while (--sz);

    because of the initial "empty-loop" test (conditional jumps
    were bad, and forward conditional jumps were worse).
    So where at least one iteration was guaranteed the do-while
    loop was a better choice.

    Also I've been told there were compilers that if using
    for or while loops the generated code was

    <initialize>
    L1:
    <evaluate condition>
    je L2
    <body>
    jmp L1
    L2:

    Instead the do-while loop would have been

    L1:
    <body>
    <evaluate condition>
    jne L1

    I.e. the code was better *for each iteration* (one conditional
    jump instead of one conditional jump and one inconditional jump).

    I think compiler got better since then, even if I don't think
    they already so smart to be able to infer the "one interation
    guaranteed" property to avoid the initial test that often.

    Andrea
    Andrea Griffini, Jun 18, 2005
    #7
  8. In article <>,
    Andrea Griffini <> wrote:

    > On Sat, 18 Jun 2005 13:35:16 -0000, Grant Edwards <>
    > wrote:
    >
    > >AFAICT, the main use for do/while in C is when you want to
    > >define a block of code with local variables as a macro:

    >
    > When my job was squeezing most out of the CPU (videogame
    > industry) I remember that the asm code generated by
    >
    > while (sz-- > 0)
    > {
    > /* do some stuff */
    > }
    >
    > was indeed worse than
    >
    > do
    > {
    > /* do some stuff */
    > } while (--sz);
    >
    > because of the initial "empty-loop" test (conditional jumps
    > were bad, and forward conditional jumps were worse).
    > So where at least one iteration was guaranteed the do-while
    > loop was a better choice.


    Hmm. I don't know what compiler you were using, but in my experience
    it's fairly typical to compile while(<test> ...) { <body> ... } as

    j test
    body: <body>
    ...
    test: <test>
    ...
    je body ;; or whatever your condition is

    To turn this into a do { <body> ... } while(<test> ...), you need only
    remove the initial "j test" instruction. Even if forward conditional
    jumps are bad for your particular architecture, this method avoids the
    problem neatly.

    Personally, I'd be pretty suspicious of the quality of a compiler that
    produced radically different code for these two constructs without some
    damned good reason.

    -M

    --
    Michael J. Fromberger | Lecturer, Dept. of Computer Science
    http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
    Michael J. Fromberger, Jun 19, 2005
    #8
  9. Peter Otten wrote:

    >I found 136 occurrences of "do {" versus 754 of "while (" and 1224 of "for
    >(" in the Python 2.4 source, so using these rough estimates do-while still
    >qualifies as "rarely used".
    >
    >Peter
    >
    >
    >

    That's 136 times you'd have to use an ugly hack instead. I definitely
    wouldn't mind an until or do/while.
    Joseph Garvin, Jun 19, 2005
    #9
  10. Remi Villatel

    Ron Adam Guest

    Joseph Garvin wrote:
    > Peter Otten wrote:
    >
    >> I found 136 occurrences of "do {" versus 754 of "while (" and 1224 of
    >> "for
    >> (" in the Python 2.4 source, so using these rough estimates do-while
    >> still
    >> qualifies as "rarely used".
    >>
    >> Peter
    >>
    >>
    >>

    > That's 136 times you'd have to use an ugly hack instead. I definitely
    > wouldn't mind an until or do/while.


    I would happy with just having while: without a 1 or True to indicate a
    continuous loop. Having a if-break at the end doesn't bother me at all.

    while:
    <suite>
    if <condition>: break

    Being able to move the break point to where it's needed or have more
    than one is a feature and not a problem. IMHO of course.

    It also has the benefit that you have the option to do an extra bit of
    cleanup between the if and the break if you need to. The until or
    do-while doesn't give you that option.

    I suppose if an until <condition>: <suite>, could be made to be more
    efficient and faster than an if <condition>: <suite>; break, then I'd
    be for that.

    while:
    <suite>
    until <condition>[: suite] # optional suite or block
    <suite>

    But I doubt it would be significantly faster than an if statement with a
    break. So the only benefit I see is you don't have to use the break
    keyword, and the exit conditions will stand out in blocks with a lot of
    if statements in them.

    Regards, Ron
    Ron Adam, Jun 19, 2005
    #10
  11. Remi Villatel

    John Roth Guest

    See Pep 315. was: Re: Loop until condition is true

    See Pep 315, which is still open, and targeted at 2.5.
    It survived the recent spate of PEP closings and rejections.

    http://www.python.org/peps/pep-0315.html

    John Roth

    "Remi Villatel" <maxilys@SPAMCOP_tele2.fr> wrote in message
    news:d90bej$c0a$...
    > Hi there,
    >
    > There is always a "nice" way to do things in Python but this time I can't
    > find one.
    >
    > What I'm trying to achieve is a conditionnal loop of which the condition
    > test would be done at the end so the loop is executed at least once. It's
    > some way the opposite of "while".
    >
    > So far, all I got is:
    >
    > while True:
    > some(code)
    > if final_condition is True:
    > break
    > #
    > #
    >
    > What I don't find so "nice" is to have to build an infinite loop only to
    > break it.
    >
    > Is there a better recipe?
    >
    > --
    > ==================
    > Remi Villatel
    > maxilys_@_tele2.fr
    > ==================
    John Roth, Jun 19, 2005
    #11
  12. Remi Villatel

    D H Guest

    Joseph Garvin wrote:
    > Peter Otten wrote:
    >
    >> I found 136 occurrences of "do {" versus 754 of "while (" and 1224 of
    >> "for
    >> (" in the Python 2.4 source, so using these rough estimates do-while
    >> still
    >> qualifies as "rarely used".
    >>
    >> Peter
    >>
    >>
    >>

    > That's 136 times you'd have to use an ugly hack instead. I definitely
    > wouldn't mind an until or do/while.


    Yeah a do while loop was proposed over 2 years ago, but nothing ever
    came of it:
    http://www.python.org/peps/pep-0315.html
    It's been discussed for many many years, and again recently:
    http://mail.python.org/pipermail/python-dev/2005-June/054167.html
    D H, Jun 20, 2005
    #12
  13. Remi Villatel

    Magnus Lycka Guest

    Remi Villatel wrote:
    > while True:
    > some(code)
    > if final_condition is True:
    > break
    > #
    > #
    >
    > What I don't find so "nice" is to have to build an infinite loop only to
    > break it.


    This is a common Python idiom. I think you will get used to it.


    > Is there a better recipe?


    final_condition = False
    while not final_condition:
    some(code)
    Magnus Lycka, Jun 21, 2005
    #13
  14. Remi Villatel

    Benji York Guest

    Cyril BAZIN wrote:
    > Another question could be: why is there not a statement "whileTrue" or
    > "loop"?


    I don't think the saving of a single space to transform "while True:"
    into "WhileTrue:" is really worth it. The same goes for "loop", the
    added complexity to the language (as little as it is) isn't rewarded by
    a large enough improvement to make it worth it.

    > It could be an economy of one unuseful test by loop.


    If by "economy" you mean "optimization", then I would suggest that the
    difference would be unnoticeable.
    --
    Benji York
    Benji York, Jun 21, 2005
    #14
  15. Remi Villatel

    Magnus Lycka Guest

    Benji York wrote:
    > If by "economy" you mean "optimization", then I would suggest that the
    > difference would be unnoticeable.


    If there is a measurable performance gain in skipping the runtime
    test in "while True", then this is a compiler issue, not a language
    issue. I don't know anything about the Python compiler internals,
    but it doesn't seem very hard to identify simple literals following
    while and if, and to skip the runtime test. (Perhaps it's done
    already?)

    I hope the time is over when language designers build ugly quirks
    into programming language syntaxes to make life a little easier for
    the compilers.
    Magnus Lycka, Jun 21, 2005
    #15
  16. Remi Villatel

    Charles Krug Guest

    On Tue, 21 Jun 2005 12:05:25 +0200, Magnus Lycka <> wrote:
    > Remi Villatel wrote:
    >> while True:
    >> some(code)
    >> if final_condition is True:
    >> break
    >> #
    >> #
    >>
    >> What I don't find so "nice" is to have to build an infinite loop only to
    >> break it.

    >
    > This is a common Python idiom. I think you will get used to it.
    >
    >
    >> Is there a better recipe?

    >
    > final_condition = False
    > while not final_condition:
    > some(code)


    To the OP, don't get hung up on the syntax we use to implement a loop.

    I took my first programming class long enough ago that we had to do
    things like this (roughly translating the FORTRAN IV I remember into
    p-code)

    To do:

    while TrueCondition
    (loop body)

    we'd have to write:

    TopOfLoop:
    if not TrueConditional goto loopEnd
    (loop body)
    goto TopOfLoop

    loopEnd:

    We were even required to write our source twice:

    The first pass was "structured" and had to be proven correct.

    The second pass was translated into something our compiler supported.

    We still called it a "while loop" even though the syntax was icky.

    When C came out with its built in looping, it was an unbelievable
    luxury.
    Charles Krug, Jun 21, 2005
    #16
  17. Remi Villatel wrote:

    > There is always a "nice" way to do things in Python but this time I can't
    > find one.
    >
    > What I'm trying to achieve is a conditionnal loop of which the condition
    > test would be done at the end so the loop is executed at least once. It's
    > some way the opposite of "while".
    >
    > So far, all I got is:
    >
    > while True:
    > some(code)
    > if final_condition is True:
    > break
    > #
    > #
    >
    > What I don't find so "nice" is to have to build an infinite loop only to
    > break it.


    checking if a logical expression is true by comparing it to True is bad
    style, and comparing values using "is" is also bad style.

    the correct way to write that if-statement is:

    if final_condition:
    break

    and yes, the "infinite" while loop is a standard Python pattern. get
    used to it.

    </F>
    Fredrik Lundh, Jun 21, 2005
    #17
  18. On 6/21/05, Magnus Lycka <> wrote:
    > I don't know anything about the Python compiler internals,
    > but it doesn't seem very hard to identify simple literals following
    > while and if, and to skip the runtime test. (Perhaps it's done
    > already?)


    True doesn't seem to be a literal, it is looked up by name and can be
    rebound to, say, 0 anytime:

    >>> import dis
    >>> o = compile('while True: pass', '<string>', 'exec')
    >>> dis.dis(o)

    1 0 SETUP_LOOP 12 (to 15)
    >> 3 LOAD_NAME 0 (True)

    6 JUMP_IF_FALSE 4 (to 13)
    9 POP_TOP
    10 JUMP_ABSOLUTE 3
    ...

    OTOH,

    >>> p = compile('while 1: pass', '<string>', 'exec')
    >>> dis.dis(p)

    1 0 SETUP_LOOP 5 (to 8)
    >> 3 JUMP_ABSOLUTE 3

    ...

    - kv
    Konstantin Veretennicov, Jun 21, 2005
    #18
  19. Remi Villatel

    Magnus Lycka Guest

    Konstantin Veretennicov wrote:
    > On 6/21/05, Magnus Lycka <> wrote:
    >
    >>I don't know anything about the Python compiler internals,
    >>but it doesn't seem very hard to identify simple literals following
    >>while and if, and to skip the runtime test. (Perhaps it's done
    >>already?)

    >
    >
    > True doesn't seem to be a literal, it is looked up by name and can be
    > rebound to, say, 0 anytime:


    Right. Silly me. Maybe in some future Python version, True and False
    will be constants, like None is since Python 2.4.
    Magnus Lycka, Jun 21, 2005
    #19
  20. Magnus Lycka wrote:
    > Konstantin Veretennicov wrote:
    >
    >> On 6/21/05, Magnus Lycka <> wrote:
    >>
    >>> I don't know anything about the Python compiler internals,
    >>> but it doesn't seem very hard to identify simple literals following
    >>> while and if, and to skip the runtime test. (Perhaps it's done
    >>> already?)

    >>
    >>
    >>
    >> True doesn't seem to be a literal, it is looked up by name and can be
    >> rebound to, say, 0 anytime:

    >
    >
    > Right. Silly me. Maybe in some future Python version, True and False
    > will be constants, like None is since Python 2.4.


    Actually, there is support in marshal to write True and False objects so
    I don't understand why this isn't in 2.4

    Anyway, if you can't wait for 2.5 either use 'while 1:', or pyc[1]

    Stelios

    [1] http://students.ceid.upatras.gr/~sxanth/pyc/
    Stelios Xanthakis, Jun 21, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zenobia
    Replies:
    0
    Views:
    304
    Zenobia
    Jul 8, 2004
  2. -
    Replies:
    12
    Views:
    688
    Remon van Vliet
    Jun 15, 2005
  3. bdb112
    Replies:
    45
    Views:
    1,339
    jazbees
    Apr 29, 2009
  4. Bill W.
    Replies:
    13
    Views:
    291
    Phillip Gawlowski
    May 9, 2011
  5. Isaac Won
    Replies:
    9
    Views:
    371
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page