POV-Ray : Newsgroups : povray.general : fundamental question re: #switch/#case/#break : fundamental question re: #switch/#case/#break Server Time
29 Jul 2024 18:19:46 EDT (-0400)
  fundamental question re: #switch/#case/#break  
From: Kenneth
Date: 22 Nov 2010 05:45:00
Message: <web.4cea483e200d2fc3196b08580@news.povray.org>
[relates to v3.6.1]

In trying to use #switch and #case in a #while-loop--and NOT using #break
in-between the several #case clauses--I get behavior that I don't understand (a
long-standing puzzle to me, actually.) That is, I don't understand the behavior
if I'm correctly reading the documentation at 3.2.2.6.3

Here's my simple code example :

#declare counter = 1;
union{
#while(counter <=3)
box{0,1
 #switch(counter)
 #case(1) scale 1
 #case(2) scale .5
 #case(3) scale .3
 //#else
 #end
 translate 1*counter*y
   }
#declare counter = counter + 1;
#end
 texture{...}
     }

I assume that I'm using #switch and #case correctly(?) in that I can use a
variable for #switch(), while giving each #case() its own exclusive set value to
compare it to.

What I expected the code to do was simply scale each box individually, and only
once, according to the value of counter. Seems simple enough--but that's not
what happens. As far as I can tell, box 1 is being scaled THREE times (by all
three successive #case clauses), box 2 is scaled TWICE, and box 3 is scaled
once. (My reason for leaving out the #break clauses is basic laziness--less
typing--but more than that, the docs tell me that they aren't necessary in such
an example. Therein lies the problem, apparently; either I'm just reading the
docs WRONG WRONG WRONG, or the wording isn't clear or accurate.)

The other oddity is this: If I keep the #case() clauses as they are, but reduce
the #while loop to #while(counter <= 2), the two boxes still get scaled by
#case(3).

BTW, I took a look at section 2.5 in the WIKI, about the new uses of #break
(introduced in one of the 3.7 betas), but what I see there doesn't seem to
affect my basic question.

*HOWEVER*, while writing this post, I now see that there might be two
mutually-exclusive explanations of how #case/#break directives work, in two
different places in the WIKI:

http://wiki.povray.org/content/Documentation:Reference_Section_2.5
says, "If a clause evaluates true but no #break is specified, the parsing will
fall through to the next #case or #range and that clause conditional is
evaluated." [This seems clear to me--it's the behavior I expected, as the
wording is the same in the v3.6.1 included documentation.]

whereas at
http://wiki.povray.org/content/HowTo:Use_conditional_structures
it says, "When processing a matching #case or #range clause, if a #break is not
encountered it continues through successive #case statements whether they match
or not." [This would, I think, explain the behavior I'm seeing; so this must be
the accurate explanation.]

An honestly naive question:  Which mode of operation *should* be correct,
compared to other standard computational languages?

Ken


Post a reply to this message

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