statements in control structures (Re: Conditional Expressions don't solve the problem)
huaiyu at gauss.almadan.ibm.com
Thu Oct 25 20:30:01 CEST 2001
On Thu, 25 Oct 2001 07:18:31 GMT, Paul Winkler <slinkp23 at yahoo.com> wrote:
>In this particular case, there is, because the two exits to (A) can be
>combined into one test. I posted something like this to the other
>recent thread on primes.
>However, this probably won't satisfy you for two reasons:
>1) It's a special case - we have a known value we can use to
>initialize the list, and add code before the loop to handle that.
Yes, changing the algorithm to avoid the problem under discussion makes the
example irrelavent here. I've tried such variations for this special case
before posting the pseudocode.
>2) There's still code duplication. Instead of duplicating the
>primes.append(n) call, we're duplicating a comparison.
Exactly. And it does not matter how trivial the duplication is in this case
- it is an example.
>The problem with the syntax you described is simply that the
>conditions when it is useful are rather restrictive:
>1) There must be more than one condition to exit the loop.
>2) There must be *exactly* two possible behaviors upon loop exit.
>I suspect that #2 is likely to be a pretty fragile requirement for
>whatever you're testing.
That for-while syntax is used as an illustration that there are _at least_
two types of exits. The purpose is to show that the simple if-break answers
miss the point.
The particular syntax can handle the case where all exits which are
considered normal loop ends go to the same block of code, in analogy to what
the else-clause does. Let's call this the 1-follow-up case.
It cannot handle the more general problem, where there are multiple
follow-up codes, and multiple exits go to each of them. Let's call this the
n-follow-up case. Incidentally, the Goldilock examples do not deal with
this problem because no two exits go to the same follow-up code. It is a
0-follow-up case. I do not know any clean syntax for n-follow-up case for
n>=2. Of course they can be done using additional variables or functions,
etc, but that's beside the point.
More information about the Python-list