Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking whether four boolean variables have equal value, non-obvious?

Tags:

c#

boolean

I have four bool variables, say :

bool a=true;
bool b=false;
bool c=true;
bool d=false;

then I want to check that those four are equal. However;

Console.WriteLine(true == false == true == false);
true

Why is this happening? I think it is because of evalution order of an equation, which goes from left to right :

((true == false) == true) == false
(false == true) == false
false == false
true

then What is a proper way to check whether all N>2 boolean variables are equal?

like image 948
thkang Avatar asked Dec 31 '12 09:12

thkang


People also ask

How do you check if a variable is boolean in Python?

We can evaluate values and variables using the Python bool() function. This method is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.

What are Boolean variables in Python?

In general, a Boolean variable can have only two values - True or False. Or in other words, if a variable can have only these two values, we say that it's a Boolean variable. It's often used to represent the Truth value of any given expression. Numerically, True is equal to 1 and False is equal to 0.

How do you set a Boolean variable to be false in Python?

In Python, boolean variables are defined by the True and False keywords. The output <class 'bool'> indicates the variable is a boolean data type. Note the keywords True and False must have an Upper Case first letter. Using a lowercase true returns an error.

Does bash have True False?

There are no Booleans in Bash. However, we can define the shell variable having value as 0 (“ False “) or 1 (“ True “) as per our needs. However, Bash also supports Boolean expression conditions.


1 Answers

if(a==b && a==c && a==d)

If you have variable number of bools not only 4

var bools = new bool[] { a, b, c, d };
var areAllEqual = bools.Skip(1).All(b=>b==bools[0]);
like image 151
I4V Avatar answered Oct 25 '22 06:10

I4V