Re: for what are for/while else clauses

From: Fredrik Lundh (fredrik_at_pythonware.com)
Date: 11/21/03


Date: Fri, 21 Nov 2003 12:45:25 +0100
To: python-list@python.org

Mel Wilson 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.

imagine a dialect of Python that supports C-style goto's and labels.
in this dialect,

        while some_condition:
            some_action ()
        else:
            last_action ()

can be rewritten as

  this_statement:
        if some_condition:
            some_action ()
            goto this_statement
        else:
            last_action ()
  next_statement:

(which, of course, is exactly what Python's current compiler does, but
on the bytecode level).

"break" and "continue" can now be rewritten as "goto next_statement"
and "goto this_statement".

for "for-in" and "try-except", the code calculating the "some_condition"
value is a bit different, but the rest works in exactly the same way.

here's the for-in version:

        <set up the iterator>
    this_statement:
        <fetch next value from iterator>
        if <value found>:
            variable = <value>
            some_action ()
            goto this_statement
        else:
            last_action ()
    next_statement:

and here's the try-except version (somewhat simplified):

    this_statement:
        <enable error handling>
        some_action ()
    error_handler:
        <disable error handling>
        if <matching error occurred>:
            some_action ()
        else:
            other_action ()

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

most Python statements pass control to the next statement
when they're done.

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

me too.

</F>



Relevant Pages

  • Re: Slope Compensation and Vodka
    ... > | IIRC its associated with the sampled-data nature of the overall ... > | loop, which the standard linear analysis conveniently ignores. ... the real circuit for a control loop analysis, ... It all seems to boil down to the "choice" of modulator transfer ...
    (sci.electronics.design)
  • Re: Exploring the FOPDT Model With a Parameter Sensitivity Study
    ... primary focus is on PID control and related architectures. ... PID loops and from 50 to over 1000 measurements. ... A control person seeking to tune a loop 'might' ... And I know my posts on this newsgroup announcing the publication of the ...
    (sci.engr.control)
  • Re: The Cascade Control Architecture - controlguru post
    ... performance are cascade control and feed forward with feedback trim. ... literally nest a secondary control loop inside a primary loop as ... you _cannot_ improve the disturbance ...
    (sci.engr.control)
  • RE: New to VBA, Search for hole in numeric value using string as a cri
    ... You are setting the control to true and never checking for it's ... Then loop through those looking for the gap in numbers. ... > Dim tcrNum As Integer ... > Dim tcrPull As Integer ...
    (microsoft.public.access.formscoding)
  • Re: question about thread scheduling
    ... still have problems with rescheduling. ... In essential, it is a control problem, and I must send a voltage to the ... If the NN run in a different thread as the control loop ...
    (microsoft.public.windowsce.platbuilder)