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 ?
The equality operator ( == ) compares two variables for equality and returns True if they are equal. Otherwise, it returns False . Since a and b references the same object, they're both identical and equal.
A Boolean variable has only two possible values: true or false.
The "and" operator && takes two boolean values and evaluates to true if both are true. The "or" operator || (two vertical bars) takes two boolean values and evaluates to true if one or the other or both are true.
I may be missing something very fundamental, but I'll give it a go:
result = ( v1 == v2 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With