# Testing lots of variables with == == ==

This topic is 4963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Just a simple thing. I want to test if a bunch of variables hold the value zero. I could do it like this: if ( a + b + c ) == 0 ) But I have a hunch there's a more elegant way of doing it, something like: if ( a == b == c == 0 ) Doesn't seem to work though. Maybe it attempts to change the value of a?

##### Share on other sites
You could put it in a macro, then at least you don't have as many equal signs.

##### Share on other sites
What you do with the first test is to check if the sum is 0, so a=1, b=2, c=-3 would evaluate as true. In the second test, you first check if a is the same as b, then if the result of that is the same as c and finally if that result is 0, which isn't what you want. The test I think you want to do is a==0 && b==0 && c==0.

##### Share on other sites
check the associativity problem
let's say == is right associative
then you got:
( a == b == c == 0 )
<=>
( a == ( b == ( c == 0 ) ))
for a=1,b=1,c=0 we get 1==(1==(0==0)) <=> 1==(1==1) <=> 1==1 <=> 1

same for left associativity(only you have to set a=0,b=1,c=1

P.S.And == is associative
P.S.P.S. Check a==0 && b==0 && c==0 or,better !(a||b||c)

##### Share on other sites

if((a | b | c) == 0)

##### Share on other sites
if( !(a || b || c || ...))

##### Share on other sites
Quote:
 Original post by bakery2k1What about:if((a | b | c) == 0)

na, using my solution gives the compiler the option to early out, if you use bitwise instead of logical or all arguments would need to be evaluated.

##### Share on other sites
@DigitalDelusion
so what.It goes out on a!=0 or b!=0 or c!=0 which is abs.normal

##### Share on other sites
Quote:
 Original post by vallentin@DigitalDelusionso what.It goes out on a!=0 or b!=0 or c!=0 which is abs.normal

let's generalize a bit and make a,b and c into functions returning int's instead and lets see what happens:

a() | b() | c()
will always call all three functions
a() || b() || c()
will only evaluate until any function returns a non zero value

if you mean logical or use logical or, this falls squarely under the KISS principle.

: also note that one of them has a clearly defined evaluation order.

##### Share on other sites
I said at the beginning this was just a simple thing... clearly I was mistaken!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 19
• 15
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015509
×