This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

AND and OR logical operator priority  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Understood ^^ Thank you!
 
I am displeased. You are no longer allowed to read this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!