|
|
Am 10.11.2017 um 22:15 schrieb Kenneth:
> My basic question is: When is the #switch() value considered an actual Boolean
> true/false value, vs. a 'simple numerical' value?
Technically, the value in a `#switch` construct is /always/ compared
numerically to those in the `#case` statements. I.e. even if you
explicitly specify `true` anywhere there, it is interpreted as 1, and
will /not/ match any other nonzero value.
>
> Getting back to basics-- e.g., NOT using the strcmp() string-compare function to
> complicate things-- consider the following:
>
> #declare C = 27;
>
> (A)
> This works successfully...
> #switch(1) // Boolean 'true'
> #case(C = 27) // a Boolean comparison that's also 'true'
The comparison operators return "genuine" `true` or `false`, i.e. 1 or 0.
> (B)
> This ALSO works... two 'numerical values' that match
> #switch(27)
> #case(C)
Yup. Of course this works.
> (C)This does NOT work (it falls through to the #else clause)...
> #switch(1000) // or 343 or -89 or any other non-zero value
> #case(C = 27) // the Boolean comparison again
This does not work, because "genuine" `true` = 1 is /not/ equal to 1000.
> If I could just get past *these* misunderstandings, my world would be a MUCH
> happier place to be in ;-)
I'd also like to re-iterate that the above constructs are all
"upside-down" usages of the `#switch` statement. The intentional use is
to specify a variable expression-to-be-examined in the `#switch`
statement itself, and constant expressions-to-match in the `#case` (or
`#range`) statements. E.g.:
#switch(Count)
#case(0)
#debug "None.\n"
#break
#case(1)
#debug "One.\n"
#break
#case(2)
#debug "Two.\n"
#break
#range(3,5)
#debug "Several.\n"
#break
#else
#debug "Plenty.\n"
#break
#end
Post a reply to this message
|
|