|
![](/i/fill.gif) |
"Kenneth" <kdw### [at] earthlink net> wrote:
> Something like,
> "For example, if you use a series of #case or #range clauses with no #breaks
> in-between, the first clause that evaluates TRUE will force all subsequent
> clauses to evaluate TRUE as well (regardless of their intrinsic TRUE/FALSE
> conditional state), in a sort of cascade--until a #break/#else/#end is
> encountered." Or something similar.
>
And this 'cascade effect' can also affect the behavior of #else, kind of a
'gotcha' (as I discovered, now that I halfway-know what I'm doing!) I'll use
the "N=prime" code as an example (with a few additions of my own)...
#declare N = 6;
#switch(N)
#case(2)
// if 2, do this
#debug "I like this number a lot!\n"
#case(3)
#case(5)
#case(7)
// if 2, 3, 5, or 7, do this
#debug "N is prime\n"
#break
#range(0,10)
#debug "N is non-prime\n"
#range(20,30)
#debug "range(20,30) is also TRUE\n"
#range(40,50)
#debug "range(40,50) is also TRUE\n"
// #break
#else // do this for anything over 10
#debug "Oops, I don't know the primes >10 by heart\n"
#end
Note that I purposely commented-out the final #break clause. (I say this because
initially, I didn't think it was necessary. But it is.)
The docs about #else say, "It is only executed if the clause before it was a
false clause."
Let's say N=6. The first clause to be evaluated TRUE is #range(1,10). Now
without thinking, it might appear that the final #range(40,50) would be
evaluated false (because N doesn't fall in its range). But because of the
'cascading TRUES' of the #ranges above it, it's actually TRUE. And, continuing,
the #else conditional in this case is also forced TRUE (if that's the correct
term), and #else's token is also printed to messages. So the #break clause
before #else is a necessity, to get an expected outcome from it.
What this shows me is that #else is 'just another conditional', not the *special
keyword with its own rules* that I've always thought it to be. (In a manner of
speaking, I thought #else had its own 'implied' #break preceding it.)
I learn something new every day...
Ken
Post a reply to this message
|
![](/i/fill.gif) |