Better ways to find if both variables are true or both are false

user679737 picture user679737 · Mar 28, 2011 · Viewed 17.1k times · Source

I have two variables which can be either true or false. I get these by doing query on database for presence or absence of certain product ids.

Now I need to set another variable which will be true or false. it will be true value when both the variables are true or both are false. It will be false value of one is true and other is false.

at present I take care of it using if statement

if ( v1 == true && v2 == true )
 result = true;
else if ( v1==false && v2 == false )
 result = true;
else if ( v1 == true && v2 == false )
 result = false;
else if ( v1==false && v2 == true )
 result = false;

Is there exists a better way of doing this ?

Answer

DXM picture DXM · Mar 28, 2011

I may be missing something very fundamental, but I'll give it a go:

result = ( v1 == v2 );