• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AND and OR logical operator priority

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I read in more than one book that the logical operator AND && takes priority over the logical operator OR ||
So in this code fragment:

line 1 should be executed this way:

b change its value to 1 and it's equal to 1, c do the same, both expression are true so the whole expression is true. Since I'm using short-circuit operators, the last part should not be executed. Right?
The line should print "a=10 b=1 c=1 bool=true"
But this is what I get: "a=1 b=200 c=40 bool=true"
So he firstly check the code that is not supposed to be executed, then since the operators the part that should be executed is not.
And if I change the original line 1 to
I get the expected output. So it seems to me that he is evaluating the expression simply in the order they are written, with AND and OR having the same priority, just like + and - or * and /.

Where is the error?

Thank you in advance
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, welcome to the ranch !! ...

Raffaele Sorrentino wrote:
I read in more than one book that the logical operator AND && takes priority over the logical operator OR ||



The logical AND operator has a higher precedence over the logical OR operator. There is no priority relationship between them.

Raffaele Sorrentino wrote:
So in this code fragment:

line 1 should be executed this way:

b change its value to 1 and it's equal to 1, c do the same, both expression are true so the whole expression is true. [REST OF EXPLANATION DELETED]



No. Precedence and Order of Evaluation are two different things. Don't mix it up!!

Yes. The AND operator had higher precedence, so the expression is like so...



but precedence doesn't affect the order of evaluation, which is defined by the JLS, as basically going from left to right (and adjusting for short circuit operators).

Again... Precedence and Order of Evaluation are two different things. Don't mix it up!!

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raffaele Sorrentino wrote:So it seems to me that he is evaluating the expression simply in the order they are written, with AND and OR having the same priority, just like + and - or * and /.



Take addition and multiplication... multiplication has higher precedence than addition. So this...



is similar to ...



however, that doesn't change the evaluation order. Java still evaluates it from left to right (using temporary memory as needed).

Henry
 
Raffaele Sorrentino
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the fastest reply ever ;)
So in my example, whatever parentheses I use, the expression will be always evaluated from left to right, in the order they were write. Right?
Sorry if I'm not, I'm really trying to understand >.<
What does it mean "&& has higher precedence over ||" if when I write || before &&, || is executed before &&? What are the advantages && gains over || if he has higher precedence?

But parentheses can override operators precedence, so
int a=5+3*7 --> int a=5+21 --> int a=26, but
int a=(5+3)*7 --> int a=8*7 --> int a=56
Right?

Sorry if there are some mistakes in my English, I'm from Naples
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raffaele Sorrentino wrote:
So in my example, whatever parentheses I use, the expression will be always evaluated from left to right, in the order they were write. Right?



While the statement is true, it wasn't the point I was making. I was using parenthesis to show how the precedence affects the results... basically, since multiplication is higher in precedence than addition, this...



and



are basically the same thing.

Raffaele Sorrentino wrote:
What does it mean "&& has higher precedence over ||" if when I write || before &&, || is executed before &&? What are the advantages && gains over || if he has higher precedence?



Since logical AND is higher in precedence than logical OR, this...



and



are basically the same thing. Precedence affects the result -- and is not related to the order of evaluation.


And I don't know what you mean by "the advantages && gains over ||". Multiplication has higher precedence over addition, and that is simply how it is; there is no advantage or gains with having multiplication with higher precedence than addition.

Henry
 
Raffaele Sorrentino
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Understood ^^ Thank you!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic