-4

Is it logically the same to do the following:

if ( a || b || c || d) 

vs

if ((a || b) || (c || d))

In other words, how does the order of the && and || operators work, left to right or right to left?

How about:

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

vs

if ((a && b) && (c && d))
  • All arguments of operators are always evaluated left to right. Precedence is a different matter (that's about choosing which operators to start with). – biziclop Nov 24 '15 at 18:34
  • 4
    @bizclop All *operands* are evaluated left to right. Operators are evaluated according to their associativity and precedence. – user207421 Nov 24 '15 at 18:36
  • @EJP Yeah, I clarified that statement. It's just a commonly-held misconception that right-associativity means evaluating from right-to-left. – biziclop Nov 24 '15 at 18:37
  • http://introcs.cs.princeton.edu/java/11precedence/ ? – Claudiu Nov 24 '15 at 18:41
  • 1
    @bizclop It's not a 'commonly-held misconception' at all. It's true. See the [JLS](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26): "There are 12 assignment operators; all are syntactically right-associative (they group right-to-left). Thus, a=b=c means a=(b=c), which assigns the value of c to b and then assigns the value of b to a." – user207421 Nov 24 '15 at 18:48
  • I think you need to understand the truth table for logical AND and OR as well as the go through the operator precedence table. I did provide a detailed answer with list of references to read in order to clear your confusions of operators. – Raf Nov 24 '15 at 19:45

4 Answers4

1

A sequence of identical operators is evaluated left to right if left-associative, or right to left if right-associative. So for example

a || b || c

is the same as

(a || b) || c

Howver if you mix || and && then operator precedence takes over.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

I would like to provide a detailed answer in order for you not only understand the Logical (AND) and (OR) but, also how the operators in general are evaluated and what are their order of precedence. There are many ways to evaluate multiple logical expressions.

The first step is to understand the truth table for OR and AND shown below (Source):

enter image description here

As shown above, for different values of x and y the X AND Y (X && Y) and X OR Y (X || Y) yield different values. If you pay attention below AND operation requires both X and Y to be true in order for the result to be true however in case of OR if either X or Y is true then the result is true.

You need to know the above table for AND and OR by heart.

If you wish to be able to evaluate multiple logical expressions (ANDs and ORs), De Morgen's law can help you how to do it using NOT operation.

The rules can be expressed in English as:

The negation of a conjunction is the disjunction of the negations. The negation of a disjunction is the conjunction of the negations.

or informally as:

"not (A and B)" is the same as "(not A) or (not B)"

also,

"not (A or B)" is the same as "(not A) and (not B)".

Read more on De Morgen's law and how to evaluate multiple logical expressions using it in here and here.

Precedence of logical AND and OR are left to right. See below table (Source) and that should give you an idea which operators are right to left and which ones are left to right as well as order of their precedence for example () and [] has the highest precedence and assignments have the lowest precedence among operators. If you notice

enter image description here

back to your example

if ( a || b || c || d)

According to truth table there are 16 possible combinations starting from

a=true,b=true,c=true,d=true 
...
... 
... 
a=false,b=false,c=false,d=false 

As long as any of the a,b,c,d are true then outcome is true hence, out of 16 possible combinations 15 will be true and last one will be false because all a,b,c,d are false.

ON whether below is same as above? Yes.

if ((a || b) || (c || d))

The only difference between the 2 is that in the second one the brackets are evaluated first hence (a || b) is evaluated then ORd with evaluation result of (c || d) but. The same logic true for above is true here too. As long as one of a,b,c,d are true then outcome is true.

In regards to below, yes the two evaluate to same outcome.

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

Same as above 16 possible combinations for different values for a,b,c,d. The only true outcome for the above is when all a,b,c,d are true. Hence only one true and another 15 false.

Raf
  • 7,505
  • 1
  • 42
  • 59
0

Both statement if ( a || b || c || d) and if ((a || b) || (c || d)) will return true if either of the variables is true.

And also statement if ( a && b && c && d) and if ((a && b) && (c && d)) will return true only if none of the variables is false.

Also evaluation is from left to right.

Collins Abitekaniza
  • 4,496
  • 2
  • 28
  • 43
0

These are short-circuiting operators that operate left-to-right, meaning as soon as the logical value of the whole expression can't be changed by the remaining terms, execution halts. As soon as || hits a true, it stops and the whole thing is true, and as soon as && hits a false, it the whole thing is false. So even though these operators are theoretically associative and commutative, order and grouping does have a practical effect. In fact, it's common to do something like:

if (foo == null || foo.someProperty())

or

if (foo != null && foo.someProperty())

Both of these are guaranteed not to have a null pointer exception because if foo is null, the second one doesn't get evaluated.

MattPutnam
  • 2,927
  • 2
  • 17
  • 23