Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if several booleans have the same value

I have three boolean variables. I need to check if all three are true or all three are false.

I can do this in a 'dummy' way:

bool areSame = false; 
if(a && b && c)    
    areSame = true; 
else if (!a && !b && !c)
    areSame = true;

I'd like to know if there is another more elegant solution.

like image 905
superM Avatar asked Dec 01 '25 05:12

superM


1 Answers

You can use the equality operator on booleans too:

bool areSame = (a == b) && (a == c);
like image 139
Douglas Avatar answered Dec 02 '25 19:12

Douglas