Question

According to wiki the precedence level of AND is higher than OR.

I was wondering, if i have a clause stated as a || b && c

How it should be computed first? (a||b) or (b && c)?

Était-ce utile?

La solution 2

AND has a higher precedence over OR in JAVA. So (b && c) should be computed before (a||b) in expression a || b && c

Check this for complete list:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Snapshot taken from the link:

enter image description here

Autres conseils

Due to precedence, it evaluates as

(a || ( b && c))

And if you add parentheses (manually), parentheses are evaluated before anything else and changes everything.

if you write

   ((a || b) && c)

That matters!

You should just try it out in a test script, but if the wiki says AND has higher precedence then I would assume b&&c is called first.

Because AND has a higher order precendence, (b && c) would be evaluated first, then the result of that would be ORed with a. In other words this is equivalent to a || (b && c)

This has very straight forward answer, it evaluate b&&c first simply because AND operation has higher precedence in the order than OR operation.

Let me explain with a more known example about precedence,

Let's take 5+2*2

We all know 2*2 is the first to evaluate and then the 5 will be added to the 4(2*2). Likewise there is order of evaluation in logic's too and that's a standard you have to follow. :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top