Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 993
user679737 Avatar asked Mar 28 '11 06:03

user679737


People also ask

How do you check if two variables are true?

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.

Can a boolean be both true and false?

A Boolean variable has only two possible values: true or false.

Does or return true if both are true?

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.


1 Answers

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

result = ( v1 == v2 ); 
like image 195
DXM Avatar answered Oct 13 '22 14:10

DXM