4

I know that using And (&&) and OR (||) in a condition statement shouldn't be used without parentheses.

So if you should use both conditions you should do (A && !B) || (C && D)

However, in some code I saw that they are not using Parentheses? What would happen then? I thought that didn't compile:

A && !B || C && D

I guess that it would resolve as with SUMS or MULTIPLICATIONS, I mean, resolve them as they are read:

(((A && !B) || C) && D)
htafoya
  • 18,261
  • 11
  • 80
  • 104
  • There's an order of operations for all symbols: +, *, &, ||, etc., as there woiuld have to be: see [this](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) But when in doubt or to ensure clarity, parentheses are a good idea. – DSlomer64 Oct 21 '15 at 17:23

1 Answers1

9

And (&&) has precedence over or (||) in the order of operations. So, this

A && !B || C && D

Is entirely equivalent to

(A && !B) || (C && D)
elixenide
  • 44,308
  • 16
  • 74
  • 100
  • 1
    And since `!` has precedence over both `&&` and `||`, it's also equivalent to `(A && (!B)) || (C && D)`. – DSlomer64 Oct 21 '15 at 17:28