Re: for what are for/while else clauses

From: Mel Wilson (mwilson_at_the-wire.com)
Date: 11/20/03

  • Next message: Mel Wilson: "Re: Python's simplicity philosophy"
    Date: Wed, 19 Nov 2003 21:14:27 -0500
    
    

    In article <mailman.787.1069069214.702.python-list@python.org>,
    "Fredrik Lundh" <fredrik@pythonware.com> wrote:
    >for a while-statement, the controlling condition is the test at
    >the top.
    >
    >for a for loop, the condition is "is there another item" (to quote the
    >language reference: "When the items are exhausted (which is imme-
    >diately when the sequence is empty) ... the loop terminates.".
    >
    >for a try-except clause, the condition is "did the suite raise an
    >exception".

       Interesting way to think about it. Thanks.

       So in some sample code:

            while some_condition:
                some_action ()
            else:
                last_action ()
            following_code ()

    If the loop results in some_action being done, say, 17
    times; then that means that some_condition was found true 17
    times. The 18th time, some_condition is found to be false,
    the else takes effect and last_action gets done one time.
    The only wrinkle then is that the while loop construct
    passes control to the following code after that one
    last_action. But we expect that from a while loop.

       The effect of a break in the suite controlled by the
    while is to blow away execution of the whole while
    construct, including the else.

       As an explanation, I like it.

            Regards. Mel.


  • Next message: Mel Wilson: "Re: Python's simplicity philosophy"

    Relevant Pages

    • Re: Need Help: compiling old Fortran program
      ... the DO-loop varable within the range of the loop. ... compilers, there are two ways of controlling a do-loop, DO ... initialising the index to i1-i3 before the looping part, ...
      (comp.lang.fortran)
    • Re: how do people feel about exit function from loop
      ... the purpose of controlling a loop, ... it is easy to adjust the conditions where the flag gets ... and it is useful following the loop for deciding what to do next. ... I am not opposed to exit for, in fact I quite like Larry's solution to the OP's ...
      (microsoft.public.vb.general.discussion)
    • Re: how do people feel about exit function from loop
      ... for the purpose of controlling a loop, ... it is easy to adjust the conditions where the flag ...
      (microsoft.public.vb.general.discussion)
    • Re: Need Help: compiling old Fortran program
      ... the DO-loop varable within the range of the loop. ... In the history of ... compilers, there are two ways of controlling a do-loop, DO ...
      (comp.lang.fortran)
    • Re: for what are for/while else clauses
      ... I'm not sure what you mean by "the controlling condition" in this case. ... print 'Purchased:', purchase ... "current_value < threshold" conditions that controls the 'while'. ...
      (comp.lang.python)