POV-Ray : Newsgroups : povray.advanced-users : problem using ORs and strcmp() in #switch/#case : Re: problem using ORs and strcmp() in #switch/#case Server Time
29 Apr 2024 08:41:05 EDT (-0400)
  Re: problem using ORs and strcmp() in #switch/#case  
From: clipka
Date: 8 Nov 2017 07:53:52
Message: <5a02fe60$1@news.povray.org>
Am 08.11.2017 um 05:40 schrieb Kenneth:

> I didn't know that using #cases without #breaks was possible :-O  Thanks! That's
> the construction I will probably use; it's nice and elegant. In fact, so
> interesting that a note in the documentation about it would be justified ;-)

It's hinted at very briefly in the v3.7 docs.

> BTW, I almost never use #switch(true) in my scenes, usually just #switch(1) or
> #switch(0)-- believing that they are the 'logical equivalents' of true/false,
> not just the 'number values' 1 and 0. It has always worked for me, but is my
> assumption valid, in light of all that's been said?

`#switch(0)` and `#switch(1)` are perfectly equivalent to
`#switch(false)` and `#switch(true)` respectively, but for code clarity
I would recommend the former when you're dealing with actual numbers
(such as the return value of `strcmp()`), and the latter when you're
dealing with logical expressions (such as the result of the `=` operator).


> Another question, just to clarify something: Is there a difference between the
> following two examples? (I don't actually code the latter way-- it may not even
> be appropriate-- but I've seen various types of non-strcmp() constructions in
> the newsgroups that look similar)...
> 
> #switch(0)
> #case(strcmp(IMG_TYPE,AA))
> 
> and
> 
> #switch(0)
> #case(strcmp(IMG_TYPE,AA)=0)

Yes, absolutely!
If IMG_TYPE is equal to AA, then `strcmp(IMG_TYPE,AA)` will yield 0.
Comparing that result with zero will yield true, i.e. some non-zero number.

So with IMG_TYPE=AA, the first case would resolve to:

    #switch(0)
    #case(0) // representing `zero difference`
      // (we'll enter here, because 0 = 0)

while the second case would resolve to:

    #switch(0)
    #case(1) // representing `true`
      // (we'll NOT enter here, because 0 <> 1)

> I ask because the latter doesn't work--no error message of any kind is created,
> but it falls through to the #else clause, even though strcmp(IMG_TYPE,AA) is a
> 'match' between IMG_TYPE and AA.

The latter case would generate an error or at least a warning in most
contemporary languages, as it is now considered good practice to clearly
distinguish between boolean an numeric values. Even in C, where use of
`int` for boolean values has a long-standing tradition, a dedicated
boolean type has been added by now.

However, POV-Ray's rusty SDL is particularly poor at distinguishing
between values representing entirely different things (another example
being colours vs. vectors), so it can't warn you that you might have a
problem there.


Post a reply to this message

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