How C # evaluates AND OR expression without parentheses

not sure if that makes sense at all im trying to figure out how c # handles the following logic

false && true || false false || true && false 

I'm basically trying to figure out how C # evaluates this expression when there are no parentheses.

+4
source share
8 answers

The compiler shows this because the standard indicates the priority of the statement .

However, if the expression requires you to think more or less about what is happening in which sequence ... use parentheses to make them understand =)

+4
source

&& has a higher priority than || so he first appreciated. Effectively they are equivalent:

 false && true || false => (false && true) || false => false false || true && false => false || (true && false) => false 

If you are not sure, use parentheses. They have no real negative impact, and everything that makes the code more readable is usually good.

Perhaps the best example (so the results will be different):

 true && false || false => (true && false) || false => false true || false && false => true || (false && false) => true 
+9
source

C # statements show operator priority:

 false && true || false = (false && true) || false = false false || true && false = false || (true && false) = false 

&& (logical AND) has a higher priority than || (logical OR)

NOTE : it’s good practice (some may say that it’s best) to always use parentheses to group logical expressions, so the intention is unambiguous ...

+3
source

Everyone talked about operator precedence and lookup tables where you can see this. But I would like to give a hint how to remember this. If you think of false as 0 and true as 1 , then && as a multiplication, and || similar to addition (they are actually called logical multiplication and logical addition). The priority relation is the same: multiplication is higher than addition. It works the same way:

 0 * 0 == 0 | false && false == false 0 * 1 == 0 | false && true == false 1 * 0 == 0 | true && false == false 1 * 1 == 1 | true && true == true 0 + 0 == 0 | false || false == false 0 + 1 == 1 | false || true == true 1 + 0 == 1 | true || false == true 1 + 1 == 1* | true || true == true 

(*) actually 2 limited to 1

And usually when in doubt, use parentheses.

+2
source

Operators will be ranked in order of operator priority .

So basically AND before OR s. Your examples are the same as:

 (false && true) || false false || (true && false) 
0
source

Most importantly ... C # uses short circuit operators. So the whole thing is false link text

0
source

operator priority short circuit rating parenthesis left to right.

-1
source

The AND operator has higher priority than the OR operator (see http://msdn.microsoft.com/en-us/library/6a71f45d.aspx ). The value of && is always evaluated first.

Thus, x && y || z x && y || z is understood as (x && y) || z (x && y) || z .

And a || b && c a || b && c is understood as a || (b && c) a || (b && c) .

-1
source

Source: https://habr.com/ru/post/1332283/


All Articles