POV-Ray : Newsgroups : povray.general : fundamental question re: #switch/#case/#break Server Time
19 Nov 2026 10:17:40 EST (-0500)
  fundamental question re: #switch/#case/#break (Message 1 to 28 of 28)  
From: Kenneth
Subject: fundamental question re: #switch/#case/#break
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

From: Le Forgeron
Subject: Re: fundamental question re: #switch/#case/#break
Date: 22 Nov 2010 06:49:25
Message: <4cea58c5$1@news.povray.org>
Le 22/11/2010 11:40, Kenneth a écrit :
> An honestly naive question:  Which mode of operation *should* be correct,
> compared to other standard computational languages?
> 
Both are correct and doing as usual in povray.
(They say the same for me)
The first one is nevertheless more prone to error of interpretation
(because "clause conditional" does not refer to #case/#range (clause)
but to the block (conditional) that is under the #case/#range clause,
even if false)

Traditional computational languages behave the same way:
 when no break, the instructions continue on the following lines.
It's an old tradition.
(a switch/case/break can be rewritten with if/table & goto... including
a goto to exit the switch block (as break): no break, no jump.


Post a reply to this message

From: Jim Holsenback
Subject: Re: fundamental question re: #switch/#case/#break
Date: 22 Nov 2010 08:17:29
Message: <4cea6d69$1@news.povray.org>
On 11/22/2010 06:40 AM, Kenneth wrote:
> *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.]

If you're using 3.7betas (for some reason i think/thought you were)
there has been a change in behavior noted here:

http://wiki.povray.org/content/Documentation:Tutorial_Section_1#Changes_and_New_Features_Summary

search for (browser find) break on that page, and the link will
eventually get you to the page you mentioned above. This is our MOST up
to date documentation as with regards to v3.7 ....

> 
> 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?

this page shows last update 10June2008 (very bottom of the page) so I'd
consider this page suspect ... I didn't review the page for accuracy,
but since the last update was a bit ago I think it's a fairly safe
assumption that there is/may be some inconsistencies


Post a reply to this message

From: "Jérôme M. Berger"
Subject: Re: fundamental question re: #switch/#case/#break
Date: 22 Nov 2010 13:24:55
Message: <4ceab577$1@news.povray.org>
Le_Forgeron wrote:
> Traditional computational languages behave the same way:
>  when no break, the instructions continue on the following lines.
> It's an old tradition.
> (a switch/case/break can be rewritten with if/table & goto... including

> a goto to exit the switch block (as break): no break, no jump.
> 
	Not really. Both behaviours may be seen in traditional languages: C
and its descendants (C++, Java, etc) behave like that, but other
languages behave as if there was a break for each case (this
includes Basic, Pascal and its descendants and most functional
languages if I'm not mistaken).

		Jerome
-- 
mailto:jeb### [at] freefr
http://jeberger.free.fr
Jabber: jeb### [at] jabberfr


Post a reply to this message


Attachments:
Download 'us-ascii' (1 KB)

From: clipka
Subject: Re: fundamental question re: #switch/#case/#break
Date: 22 Nov 2010 14:45:45
Message: <4ceac869@news.povray.org>
Am 22.11.2010 11:40, schrieb Kenneth:

> *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?

The switch syntax appears to mimick the syntax of C, which also uses the 
"slip-thru" concept, i.e. if you don't insert a "break;", the next block 
of statements will be executed even if the case value doesn't match.

C++, Java, JavaScript and C# also use this concept (no surprise though, 
as they all have inherited a lot of syntax from C).

The idea behind this is that you can write stuff like:

     #switch(N)
       #case(2)
       #case(3)
       #case(5)
       #case(7)
         #debug "N is prime\n"
         #break
       #range(0,10)
         #debug "N is non-prime\n"
         #break
       #else
         #debug "I don't know the primes >10 by heart\n"
     #end

It should be noted that the Missing Break Error is a well-known source 
of bugs in C-derived languages, so the fact that it's in these languages 
is in no way a sign of quality ;-)


Post a reply to this message

From: Darren New
Subject: Re: fundamental question re: #switch/#case/#break
Date: 22 Nov 2010 15:01:19
Message: <4ceacc0f$1@news.povray.org>
clipka wrote:
> C++, Java, JavaScript and C# also use this concept 

FWIW, C# is different. Each section of the switch has to end in either a 
break or a goto, and if you want "fallthrough", you goto a different case 
label. Best of both worlds: you get to share cases if you want, but the 
cases are still independent, so you can (for example) rearrange them without 
breaking your code.

Plus, you don't get to do things like put while loops around the cases but 
inside the switch. :-)

-- 
Darren New, San Diego CA, USA (PST)
   Serving Suggestion:
     "Don't serve this any more. It's awful."


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
Date: 22 Nov 2010 20:05:00
Message: <web.4ceb0f2c2f7b65a8196b08580@news.povray.org>
Jim Holsenback <jho### [at] povrayorg> wrote:

> If you're using 3.7betas (for some reason i think/thought you were)
> there has been a change in behavior noted here:
>
>
http://wiki.povray.org/content/Documentation:Tutorial_Section_1#Changes_and_New_Features_Summary
>
> search for (browser find) break on that page, and the link will
> eventually get you to the page you mentioned above. This is our MOST up
> to date documentation as with regards to v3.7 ....

I'm still with 3.6.1c (but I assume that the *basic* operation of #case/#break
hasn't changed since then?)

If the link you mentioned is the latest WIKI documentation, it matches most of
the wording in my 3.6.1 included documentation, and is the prime source of my
puzzlement. Here's why:

The WIKI (updated 14 October 2010) at
http://wiki.povray.org/content/Documentation:Reference_Section_2.5#The_.23switch.2C_.23case.2C_.
23range_and_.23break_Directives

says, "if the [#case} clause's condition is true, that clause's tokens are
parsed normally and parsing continues until a #break, #else or #end directive is
reached. If the condition is false, POV-Ray skips until another #case or #range
is found."

and

"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."

The words 'skip' and 'evaluate' seem clear and co-consistent as to what *should*
happen (*IF* I'm understanding them correctly.) Here's how I read the quoted
statements in relation to the simple code example I posted:

Given:

#while(counter <=3) and the situation where counter actually equals 2,

I would expect that, without using any #breaks, #case(1) would be 'evaluated' as
FALSE and 'skipped' (no action there), leading to #case(2) being 'evaluated' as
TRUE, then #case(3) being 'evaluated' as FALSE and 'skipped'--with the sum total
of these evaluations being that only the #case(2) scaling (its token) is applied
to my box, as it's the only TRUE evaluation. But this isn't what happens;
instead, the behavior *seems* to match the outdated(?) WIKI section that 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." I.e., any following #case() tokens are applied whether those #cases are
TRUE *or* FALSE.

See my conundrum? Of course, my little code problem can easily be fixed with
some #break clauses. But I'm FAR more concerned with my own lack of
understanding of the logic of this situation.

Ken


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
Date: 22 Nov 2010 20:40:00
Message: <web.4ceb1b312f7b65a8196b08580@news.povray.org>
Le_Forgeron <lef### [at] freefr> wrote:
> Le 22/11/2010 11:40, Kenneth a écrit :
> > An honestly naive question:  Which mode of operation *should* be correct,
> > compared to other standard computational languages?
> >
> Both are correct and doing as usual in povray.
> (They say the same for me)
> The first one is nevertheless more prone to error of interpretation
> (because "clause conditional" does not refer to #case/#range (clause)
> but to the block (conditional) that is under the #case/#range clause,
> even if false)

If that's the case--no pun intended ;-) --then the wording certainly needs
changing and clarification, IMO. The phrase (or idea of) 'conditional clause',
and possibly the word 'evaluate,' would appear to have unintended multiple
meanings there--at least to *this* poor struggling soul. :-(


Post a reply to this message

From: Jim Holsenback
Subject: Re: fundamental question re: #switch/#case/#break
Date: 23 Nov 2010 06:59:13
Message: <4cebac91$1@news.povray.org>
On 11/22/2010 08:59 PM, Kenneth wrote:
> See my conundrum? Of course, my little code problem can easily be fixed with
> some #break clauses. But I'm FAR more concerned with my own lack of
> understanding of the logic of this situation.

Doesn't the 2nd "Note:" in the previously mentioned wiki passage,
particularly ... "(previously, #break was only useful right before the
next #case, #range or #else directive, to indicate that a slip-through
was not desired)." ... offer any clarity?


Post a reply to this message

From: "Jérôme M. Berger"
Subject: Re: fundamental question re: #switch/#case/#break
Date: 23 Nov 2010 14:08:06
Message: <4cec1116@news.povray.org>
Kenneth wrote:
> Jim Holsenback <jho### [at] povrayorg> wrote:
> 
>> If you're using 3.7betas (for some reason i think/thought you were)
>> there has been a change in behavior noted here:
>>
>> http://wiki.povray.org/content/Documentation:Tutorial_Section_1#Change
s_and_New_Features_Summary
>>
>> search for (browser find) break on that page, and the link will
>> eventually get you to the page you mentioned above. This is our MOST u
p
>> to date documentation as with regards to v3.7 ....
> 
> I'm still with 3.6.1c (but I assume that the *basic* operation of #case
/#break
> hasn't changed since then?)
> 
> If the link you mentioned is the latest WIKI documentation, it matches 
most of
> the wording in my 3.6.1 included documentation, and is the prime source
 of my
> puzzlement. Here's why:
> 
> The WIKI (updated 14 October 2010) at
> http://wiki.povray.org/content/Documentation:Reference_Section_2.5#The_
.23switch.2C_.23case.2C_.
> 23range_and_.23break_Directives
> 
> says, "if the [#case} clause's condition is true, that clause's tokens 
are
> parsed normally and parsing continues until a #break, #else or #end dir
ective is
> reached. If the condition is false, POV-Ray skips until another #case o
r #range
> is found."
> 
> and
> 
> "If a clause evaluates true but no #break is specified, the parsing wil
l fall
> through to the next #case or #range and that clause conditional is eval
uated."
> 
> The words 'skip' and 'evaluate' seem clear and co-consistent as to what
 *should*
> happen (*IF* I'm understanding them correctly.) Here's how I read the q
uoted
> statements in relation to the simple code example I posted:
> 
	You're right, there is a mistake in the doc. The first statement is
correct: if the condition is true, then parsing continues until a
#break, #else or #end (unless that #break, #else or #end is
associated with another control statement such as #if or #while).

	The second statement is wrong: when the parsing falls through to
the next #case or #range, that clause conditional is *ignored* and
the following statements are parsed as if the conditional were true.

	The problem here is with the word "conditional". The docs should
either contain a formulation similar to what I wrote above or use
the word "body" instead of "conditional".

		Jerome
-- 
mailto:jeb### [at] freefr
http://jeberger.free.fr
Jabber: jeb### [at] jabberfr


Post a reply to this message


Attachments:
Download 'us-ascii' (1 KB)

From: Thomas A  Fine
Subject: Re: fundamental question re: #switch/#case/#break
Date: 23 Nov 2010 16:23:27
Message: <4cec30cf$1@news.povray.org>
In article <web.4ceb0f2c2f7b65a8196b08580@news.povray.org>,
Kenneth <kdw### [at] earthlinknet> wrote:
>See my conundrum? Of course, my little code problem can easily be fixed with
>some #break clauses. But I'm FAR more concerned with my own lack of
>understanding of the logic of this situation.

Simplest way to look at it - successive cases with no break are all
OR'ed together.

"case" means "If this case OR any of the preceding cases in this block
since the last break are true, execute this code"

This lets you conserve code by grouping a bunch of different cases
together.  See the "N is prime" example from clipka.  But you could
also ADD code for #case(2) that only executes for that, without
separating it out of the conditions for 3,5 and 7:

       #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)
         // if 0-10 and NOT 2,3,5,7 (because of the "break" above) do this
         #debug "N is non-prime\n"
         #break
       #else
         #debug "I don't know the primes >10 by heart\n"
     #end

    tom


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
Date: 23 Nov 2010 21:00:01
Message: <web.4cec70cd2f7b65a8196b08580@news.povray.org>
Jim Holsenback <jho### [at] povrayorg> wrote:
> On 11/22/2010 08:59 PM, Kenneth wrote:
> > See my conundrum? Of course, my little code problem can easily be fixed with
> > some #break clauses. But I'm FAR more concerned with my own lack of
> > understanding of the logic of this situation.
>
> Doesn't the 2nd "Note:" in the previously mentioned wiki passage,
> particularly ... "(previously, #break was only useful right before the
> next #case, #range or #else directive, to indicate that a slip-through
> was not desired)." ... offer any clarity?

I wish I could say YES, but... :-(

"...a slip through was not desired" didn't make much sense to me either, since I
couldn't make real sense of the overall descriptions...

Although, it's now starting to sink in, finally. :-)

Ken


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
Date: 23 Nov 2010 21:10:00
Message: <web.4cec72e72f7b65a8196b08580@news.povray.org>
fin### [at] head-cfaharvardedu (Thomas A. Fine) wrote:

> Simplest way to look at it - successive cases with no break are all
> OR'ed together.
>
> "case" means "If this case OR any of the preceding cases in this block
> since the last break are true, execute this code"
>
> This lets you conserve code by grouping a bunch of different cases
> together.  See the "N is prime" example from clipka.  But you could
> also ADD code for #case(2) that only executes for that, without
> separating it out of the conditions for 3,5 and 7:

That is a beautiful and clear description (and code example) of how it all
works--MANY thanks! (It's also a great little piece of experimental code for me
--I've been playing around with it, to get a clearly-understandable 'feel' for
what goes on.) And it shows me that I sure had a muddled *quasi*-understanding
of it all, before now. Geez, I sure was clueless...

IMO, your comments and code example would make a great addition to the WIKI
(hint, hint!)

Ken


Post a reply to this message

From: Jim Holsenback
Subject: Re: fundamental question re: #switch/#case/#break
Date: 24 Nov 2010 06:24:58
Message: <4cecf60a$1@news.povray.org>
On 11/23/2010 10:05 PM, Kenneth wrote:
> IMO, your comments and code example would make a great addition to the WIKI
> (hint, hint!)

ah then ... a perfect opportunity for you to bolster your understanding
by having a go at the howto article you mentioned in your original post ;-)


Post a reply to this message

From: Thomas A  Fine
Subject: Re: fundamental question re: #switch/#case/#break
Date: 24 Nov 2010 12:10:58
Message: <4ced4722$1@news.povray.org>
In article <web.4ceb0f2c2f7b65a8196b08580@news.povray.org>,
Kenneth <kdw### [at] earthlinknet> wrote:
>See my conundrum? Of course, my little code problem can easily be fixed with
>some #break clauses. But I'm FAR more concerned with my own lack of
>understanding of the logic of this situation.

Simplest way to look at it - successive cases with no break are all
OR'ed together.

"case" means "If this case OR any of the preceding cases in this block
since the last break are true, execute this code"

This lets you conserve code by grouping a bunch of different cases
together.  See the "N is prime" example from clipka.  But you could
also ADD code for #case(2) that only executes for that, without
separating it out of the conditions for 3,5 and 7:

       #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)
         // ELSE if 0-10 do this   ("break" is sort of like "else")
         #debug "N is non-prime\n"
         #break
       #else
         #debug "I don't know the primes >10 by heart\n"
     #end

    tom


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
Date: 24 Nov 2010 13:30:01
Message: <web.4ced595b2f7b65a8196b08580@news.povray.org>
"Kenneth" <kdw### [at] earthlinknet> wrote:

> That is a beautiful and clear description (and code example) of how it all
> works--MANY thanks! (It's also a great little piece of experimental code for me
> --I've been playing around with it, to get a clearly-understandable 'feel' for
> what goes on.)

Ooh, this is embarrassing: I just realized that Clipka had posted the basic "N
is prime" code here earlier (and I wasn't paying good attention--I was too
wrapped up in minutia.) So...thanks to Thomas AND Clipka :-)  (How's *that* for
diplomacy?!)

Ken


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
Date: 24 Nov 2010 14:00:00
Message: <web.4ced5f5a2f7b65a8196b08580@news.povray.org>
Jim Holsenback <jho### [at] povrayorg> wrote:
> On 11/23/2010 10:05 PM, Kenneth wrote:
> > IMO, your comments and code example would make a great addition to the WIKI
> > (hint, hint!)
>
> ah then ... a perfect opportunity for you to bolster your understanding
> by having a go at the howto article you mentioned in your original post ;-)

Well, I guess I should have expected that ;-)

You mean the how-to article I mentioned(?) in this post (did I?)--or one of the
many *others* that I said I was gonna write, on other topics? Yeah,
well....um...

Actually, I *have* been mulling over all that's been said here so far, and
comparing it to what the docs say, and may yet have a go at trying to re-word
part of the docs/WIKI-- if only to satisfy my *own* sense of clarity.
(Admittedly from a 'non-programmer's' rather naive viewpoint.) Yet it might
prove useful to others, I suppose.  I have some ideas that are starting to
gel--but I'm realizing that indeed it *is* difficult to put these #case/#break
concepts into simpler, more succinct form. Need to think on it a bit more...

Ken


Post a reply to this message

From: clipka
Subject: Re: fundamental question re: #switch/#case/#break
Date: 24 Nov 2010 16:01:03
Message: <4ced7d0f@news.povray.org>
Am 24.11.2010 19:28, schrieb Kenneth:

> Ooh, this is embarrassing: I just realized that Clipka had posted the basic "N
> is prime" code here earlier (and I wasn't paying good attention--I was too
> wrapped up in minutia.) So...thanks to Thomas AND Clipka :-)  (How's *that* for
> diplomacy?!)

Let it be known to him that We have decided to grant him the mercy of 
accepting his apology.

:-)


Post a reply to this message

From: Stephen
Subject: Re: fundamental question re: #switch/#case/#break
Date: 24 Nov 2010 16:17:22
Message: <4ced80e2$1@news.povray.org>
On 24/11/2010 9:00 PM, clipka wrote:
>
> Let it be known to him that We have decided to grant him the mercy of
> accepting his apology.
>
> :-)

LOL


-- 
Regards
     Stephen


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
Date: 25 Nov 2010 04:25:01
Message: <web.4cee283f2f7b65a8196b08580@news.povray.org>
> <jeb### [at] freefr> wrote:

>  You're right, there is a mistake in the doc. The first statement is
> correct: if the condition is true, then parsing continues until a
> #break, #else or #end (unless that #break, #else or #end is
> associated with another control statement such as #if or #while).
>
>  The second statement is wrong: when the parsing falls through to
> the next #case or #range, that clause conditional is *ignored* and
> the following statements are parsed as if the conditional were true.
>

In addition to this correction, I believe it would be helpful to have the 2nd
paragraph explicitly state that, when using no #breaks, a *series*
of #case's or #range's can produce 'cascading TRUE' behavior. (As I found out in
my example.) The WIKI calls it a 'slip-through' if I'm not mistaken. To most
folks in the POV-Ray community this is probably obvious--from the
wording of the docs above or from other coding experience. But I just didn't
grasp it that way initially (am I alone?)

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.

That might sound like I'm 'dumbing-down' the documentation; but with such an
addition, I would probably have understood these concepts better, from the
get-go.

Ken


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
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

From: "Jérôme M. Berger"
Subject: Re: fundamental question re: #switch/#case/#break
Date: 25 Nov 2010 13:24:17
Message: <4ceea9d1@news.povray.org>
Kenneth wrote:
> "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 subseq
uent
>> clauses to evaluate TRUE as well (regardless of their intrinsic TRUE/F
ALSE
>> 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 o
f 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 thi
s 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 t
he
> 'cascading TRUES' of the #ranges above it, it's actually TRUE. And, con
tinuing,
> the #else conditional in this case is also forced TRUE (if that's the c
orrect
> term), and #else's token is also printed to messages. So the #break cla
use
> before #else is a necessity, to get an expected outcome from it.
> 
	The doc explicitly states the opposite: "parsing continues until a
#break, *#else* or #end directive is reached", so this is a bug
(either in povray or in the docs).

		Jerome
-- 
mailto:jeb### [at] freefr
http://jeberger.free.fr
Jabber: jeb### [at] jabberfr


Post a reply to this message


Attachments:
Download 'us-ascii' (1 KB)

From: Jim Holsenback
Subject: Re: fundamental question re: #switch/#case/#break
Date: 25 Nov 2010 14:42:29
Message: <4ceebc25$1@news.povray.org>
On 11/25/2010 02:24 PM, "Jérôme M. Berger" wrote:
> The doc explicitly states the opposite: "parsing continues until a
> #break, *#else* or #end directive is reached", so this is a bug
> (either in povray or in the docs).
> 
> 		Jerome

There was a bug discovered with the #break directive behavior that was
addressed (with enhancements) by Change 4942 on 2010/04/18 by
clipka@cli-pc-xp64 ... I believe things "jive" now


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
Date: 26 Nov 2010 02:30:01
Message: <web.4cef5fc32f7b65a8196b08580@news.povray.org>
> <jeb### [at] freefr> wrote:

>  The doc explicitly states the opposite: "parsing continues until a
> #break, *#else* or #end directive is reached", so this is a bug
> (either in povray or in the docs).
>

Yeah, I have to admit that I was a bit mystified by the docs (and WIKI) on this
point. But then again, I was muddying the issue somewhat by doing my test in
*old* v3.6.1--while expecting the behavior to jive with the latest WIKI info
(and beta changes.)

Ken


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
Date: 26 Nov 2010 02:55:01
Message: <web.4cef65d92f7b65a8196b08580@news.povray.org>
Jim Holsenback <jho### [at] povrayorg> wrote:
> On 11/25/2010 02:24 PM, "Jérôme M. Berger" wrote:
> > The doc explicitly states the opposite: "parsing continues until a
> > #break, *#else* or #end directive is reached", so this is a bug
> > (either in povray or in the docs).
> >
>
> There was a bug discovered with the #break directive behavior that was
> addressed (with enhancements) by Change 4942 on 2010/04/18 by
> clipka@cli-pc-xp64 ... I believe things "jive" now

Thanks, Jim. (Clipka's Change 4942 is quoted verbatim in the WIKI, I believe.)

So, just so I understand POV's *current* behavior (beta 39): The need for a
#break before #else is *no longer* necessary to avoid the 'cascading
TRUE behavior? I.e., (as a simple analogy) does #else now have an 'implied'
#break before it? Sorry, I'm still having a bit of trouble understanding change
4942 if there's NO #break before an #else.

I guess one easy way to find out would be to run my 'missing #break before
#else' code test in beta 39, to see what happens. Could..uh..someone do that?
:-)

Ken


Post a reply to this message

From: clipka
Subject: Re: fundamental question re: #switch/#case/#break
Date: 27 Nov 2010 05:09:47
Message: <4cf0d8eb@news.povray.org>
Am 26.11.2010 08:50, schrieb Kenneth:
> Jim Holsenback<jho### [at] povrayorg>  wrote:
>>
>> There was a bug discovered with the #break directive behavior that was
>> addressed (with enhancements) by Change 4942 on 2010/04/18 by
>> clipka@cli-pc-xp64 ... I believe things "jive" now
>
> Thanks, Jim. (Clipka's Change 4942 is quoted verbatim in the WIKI, I believe.)

Doesn't seem like that. Original description of change #4942 was as follows:

---------------------
[smp] modified #break semantics; the #break directive can now be used...:

- anywhere within a #case or #range block, to skip to the end of the 
#switch directive (previously, #break was only useful right before the 
next #case, #range or #else directive, to indicate that a slip-through 
was not desired);

- anywhere within a loop block (both #while and #for), to preliminarily 
terminate the loop; and

- anywhere within a #macro block, to preliminarily terminate the macro.

Example for the use in a loop:

#local R = seed(4711);
#for (I, 1, 100)
   #if (rand(R) < I/1000)
     #break // terminate loop early
   #end
   #debug concat(str(I,0,0), " iterations and counting\n")
#end

Where multiple #switch, loop and/or #macro blocks are nested, #break 
will leave only the innermost of these.
---------------------

> So, just so I understand POV's *current* behavior (beta 39): The need for a
> #break before #else is *no longer* necessary to avoid the 'cascading
> TRUE behavior? I.e., (as a simple analogy) does #else now have an 'implied'
> #break before it? Sorry, I'm still having a bit of trouble understanding change
> 4942 if there's NO #break before an #else.

The wording in the Wiki is wrong; in a #switch statement, #else acts 
like a #range that encompasses any possible number (*), i.e. the 
preceding #case or #range must be ended with a #break statement if 
execution should not continue into the #else block.

(* This analogy even goes so far that it is legal - though perfectly 
useless - to place additional #case or #range statements after an #else 
block; even additional #else blocks will not cause POV-Ray to complain. 
Note that this applies only to switch statements though.)


Post a reply to this message

From: Kenneth
Subject: Re: fundamental question re: #switch/#case/#break
Date: 27 Nov 2010 08:45:01
Message: <web.4cf10a9b2f7b65a8196b08580@news.povray.org>
clipka <ano### [at] anonymousorg> wrote:
> Am 26.11.2010 08:50, schrieb Kenneth:

> > Thanks, Jim. (Clipka's Change 4942 is quoted verbatim in the WIKI, I believe.)
>
> Doesn't seem like that. Original description of change #4942 was as follows:

Sorry, you're right. (In all the cross-referencing I was doing between various
WIKI pages/3.6.1 docs/beta-test page at POV-Ray.org, I got a bit confused!)
>
> > I.e., (as a simple analogy) does #else now have an 'implied'
> > #break before it?
>
> The wording in the Wiki is wrong; in a #switch statement, #else acts
> like a #range that encompasses any possible number (*), i.e. the
> preceding #case or #range must be ended with a #break statement if
> execution should not continue into the #else block.

Thanks; well-put, and completely clarified now. (This would make a good addition
to the WIKI, too.)

Ken


Post a reply to this message

From: Jim Holsenback
Subject: Re: fundamental question re: #switch/#case/#break
Date: 27 Nov 2010 09:15:25
Message: <4cf1127d$1@news.povray.org>
On 11/27/2010 09:41 AM, Kenneth wrote:
> clipka <ano### [at] anonymousorg> wrote:
>> Am 26.11.2010 08:50, schrieb Kenneth:
> 
>>> Thanks, Jim. (Clipka's Change 4942 is quoted verbatim in the WIKI, I believe.)
>>
>> Doesn't seem like that. Original description of change #4942 was as follows:
> 
> Sorry, you're right. (In all the cross-referencing I was doing between various
> WIKI pages/3.6.1 docs/beta-test page at POV-Ray.org, I got a bit confused!)
>>
>>> I.e., (as a simple analogy) does #else now have an 'implied'
>>> #break before it?
>>
>> The wording in the Wiki is wrong; in a #switch statement, #else acts
>> like a #range that encompasses any possible number (*), i.e. the
>> preceding #case or #range must be ended with a #break statement if
>> execution should not continue into the #else block.
> 
> Thanks; well-put, and completely clarified now. (This would make a good addition
> to the WIKI, too.)
> 
> Ken
> 
> 
> 
certainly a lot of bandwidth over something that should have been a
trivial matter ... done


Post a reply to this message

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