POV-Ray : Newsgroups : povray.general : fundamental question re: #switch/#case/#break : Re: fundamental question re: #switch/#case/#break Server Time
30 Jul 2024 00:30:07 EDT (-0400)
  Re: fundamental question re: #switch/#case/#break  
From: Kenneth
Date: 25 Nov 2010 06:15:01
Message: <web.4cee44802f7b65a8196b08580@news.povray.org>
"Kenneth" <kdw### [at] earthlinknet> 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

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.