• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logical operator precedence  RSS feed

 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Sorry one more for today

I am reading a tutorial on logical operators and it states "The associativity of the &&, ||, &, | and ^ operators is from left to right." It also includes a diagram of these operators in the order above with an arrow moving from left to right.

This does not seem to match the precedence rules I have found on a number of web sites e.g. http://www.uni-bonn.de/~manfear/javaoperators.php

I think the order of precedence should be &, ^, |, &&, ||
Can someone verify this for me (and that the tutorial is incorrect)?

Cheers,
John
 
Kristin Stromberg
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're confusing associativity with precedence. They're not the same thing.
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kristin Stromberg:
I think you're confusing associativity with precedence. They're not the same thing.


Okay thanks Kirstin. can anyone explain to me what associatity means with respect to logical operators. also what is the difference between associativity and prededence?

Cheers,
John
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Operator Precedence
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Morrow:
Operator Precedence


Hi Steve,
This just seems to cover precedence and I fully understand this. What I dont understand is associavity and how it differs from precedence.

Cheers,
John
 
Kristin Stromberg
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help... it's from A Programmer's Guide to Certification

Precedence rules are used to determine which operator should be applied first if there are two operators with different precedence, and these follow each other in the expression. In such a case, the operator with the highest precedence is applied first.

2 + 3 * 4 is evaluated as 2 + (3 * 4) (with the result 14) since * has higher precedence than +.

Associativity rules are used to determine which operator should be applied first if there are two operators with the same precedence, and these follow each other in the expression.

Left associativity implies grouping from left to right:

1 + 2 - 3 is interpreted as ((1 + 2) - 3), since the binary operators + and - both have same precedence and left associativity.

Right associativity implies grouping from right to left:

- - 4 is interpreted as (- (- 4)) (with the result 4), since the unary operator - has right associativity.

The precedence and associativity rules together determine the evaluation order of the operators.

[ June 21, 2005: Message edited by: Kristin Stromberg ]
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kristin. This fully explains the difference
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!