How Boolean expressions are evaluated en.NET?

I have this code, "emulating" a truth table where two propositions are taken (PA and PB), a proposition is constructed (PC) linking with & to the negation of PA & PB and finally the propositions made above are analyzed:

// ...
bool PA = false, PB = true; // Propocision A y B
bool PC = !(!PA & PB); // Se niega el resultado de !A & B
bool PD = PA & PB | ((PA & PB) & PC);
// El resultado de PD es false.

How are Boolean expressions evaluated in .NET?

 2
Author: Carlos Muñoz, 2016-04-08

4 answers

Expressions are evaluated from left to right respecting the parentheses

bool PA = false, PB = true; 
bool PC = !(!PA & PB); //not (not(false) and true) = false
bool PD = PA & PB | ((PA & PB) & PC); // (false and true) or ((false and true) and false) = false

Expanding the final expression:

D = A & B | ((A & B) & C)
D = false & true | ((false & true) & false)
D = false | ((false & true) & false)
D = false | (false & false)
D = false | false
D = false
 2
Author: Diego Torres, 2016-04-08 17:09:24

Logical Operators in C# are evaluated the same as in virtually all languages i.e. from left to right with higher priority los & and later los |. Parentheses, as always, change the order of evaluation.

I see some" confusion " of opinions regarding the && and ||operators. Really the only difference from & and / is that they work in "short circuit" mode i.e. they don't keep evaluating if a value is found that ends the condition.

For example: true / true, evaluates the two conditions, however true || false evaluates only the first, as it is a "logical or" any value that we compare with it will always be true (true or X == true) and therefore it is not necessary to execute the second condition.

In the case of && is similar, false & true evaluates both conditions while false && true evaluates only the first (false and x is always false).

These operators are very important to avoid errors, for example, let's imagine that obj is an object with a logical enabled property. If we do this:

obj != null & obj.Enabled

Will give us an error when obj is null, because & evaluates the two conditions and the second of them "obj.Enabled "throws an exception of the type "obj is null".

However, if we do:

obj != null && obj.Enabled

Will not skip any exception because if obj = = null, the first condition is false and therefore the second is not even evaluated. Our code in execution it does not pass through it and therefore we will never skip the exception "obj is null".

Just one more thing, in cases where we have such a comparison with methods, it is advisable to put first those that will be evaluated faster, so we get a more efficient code. For example:

if (Metodo1() && Metodo2())

If Metodo1 returns false, Metodo2 is not executed and therefore "save" the execution time of that code (of course, if Metodo1 is true, Metodo2 will be evaluated).

 2
Author: Jose Antonio Bautista, 2016-12-02 22:45:36

>>bool PC = !(!PA & PB); / / refuses the result of !A & B

The value of PA is denied, i.e. true. Then it is evaluated with PB where the result of & will be true since both are and that union is denied. The final result will be false

 0
Author: Leandro Tuttini, 2016-04-08 17:01:20

I don't know if what you want to know is why PD is false. It occurs to me that by not indicating parentheses the | is evaluated before the &. So as PA is false, whatever is to the right of The & doesn't matter. P.S. is false always.

 0
Author: Leon, 2016-04-08 17:03:49