Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Evaluation order (Enthuware test 5)

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Round 9......

Firstly, I know this doesn't compile (you can't assign true to false), but...


On the enthuware forum there seems to be a discussion about the precedence of line 1, but it doesn't matter does it?

!= has a higher precedence than =, but that isn't why b2 != b1 is evaluated first. it's just left to right evaluation, isn't it? precedence just makes the expression resolve to this:

((b2 != b1) = !b2) - which has no impact on the order of evaluation, although i'm not sure what the impact of the higher precedence of the unary operator ! is. i'm guessing none at all.

assignments in general are probably one of my weaker areas - but i know a few threads here that I can look at, was just interested in this question in particular!

Nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:On the enthuware forum there seems to be a discussion about the precedence of line 1, but it doesn't matter does it?

It does matter! Because this code snippet fails to compile due to its evaluation order If we add a pair of parantheses at the correct location, the evaluation order changes and this code snippet compiles successfully and will print true (I've added a semicolon to the last println statement as well)

nick woodward wrote:!= has a higher precedence than =, but that isn't why b2 != b1 is evaluated first. it's just left to right evaluation, isn't it? precedence just makes the expression resolve to this:

((b2 != b1) = !b2) - which has no impact on the order of evaluation, although i'm not sure what the impact of the higher precedence of the unary operator ! is. i'm guessing none at all.

You are correct on this one! Because the != operator has a higher precedence, the expression will be evaluated like (b2 != b1) = !b2. And that's exactly the reason why you'll get a compiler error: the left-hand side of the assignment operator will evaluate to a boolean value (and it must be a variable). But it's of course very hard to determine the exact evaluation order of an expression which doesn't compile, because if you remove a subexpression (so the expression successfully compiles), you might break the actual result. But the assignment operator is evaluated right to left: so first !b2 will be evaluated (to true), and then the expression b2 != b1 will be evaluated left to right: so first b2 (to false), then b1 (also to false) and finally false != false will be evaluated (again to false).

So let's focus on the expression which compiles successfully: b2 != (b1 = !b2). Evaluation order:
1/ b2 -> false
2/ !b2 -> true
3/ b1 = true -> true
4/ false != true -> true
And that's why true will be printed. If you would print the values of b1 and b2, you'll get true and false respectively.

And finally just for fun Let's change the order of the operators and operands a bit and the question remains the same: what's the result of this code snippet?

Hope it helps!
Kind regards,
Roel
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, yeah, i get what you're saying there, it does matter, but what i meant is:

(taking your example)
b2 != (b1 = !b2)

that the higher precedence subexpression (b1 = !b2) is not evaluated first. the evaluation order is still left to right in the expression as a whole, then right to left within the brackets. correct?

the discussion on the enthuware forum (i wish i could find it now) suggested that my original example:

(b2 != b1 = !b2) was evaluated ((b2 != b1) = !b2) because != has the higher precedence, which isn't correct in my opinion.
someone then took the same misunderstanding and said "well why isn't !b2 evaluated first"

I just wanted to get it clear in my head that ((b2 != b1) = !b2) was left to right evaluation regardless of precedence.



b1 = (b2 != !b2)
!= has the precedence, but again i'd argue that doesn't change the evaluation order, it just means that b2 is tied to =!b2, not b1=.
it just so happens that (b2 != !b2) is evaluated first because of right to left order of evaluation with assignments.
in fact !b2 within the subexpression should be evaluated first i think.

b1 = (false != true) => true
or:
!b2 = true
b2 = false
b2 != true ===> true
b1 <=== true

i guess!

Nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:(taking your example)
b2 != (b1 = !b2)

that the higher precedence subexpression (b1 = !b2) is not evaluated first. the evaluation order is still left to right in the expression as a whole, then right to left within the brackets. correct?

Yes! That's absolutely spot-on!

nick woodward wrote:I just wanted to get it clear in my head that ((b2 != b1) = !b2) was left to right evaluation regardless of precedence.

Maybe it's probably better to use an expression which successfully compiles to determine the evaluation order So here we go: what's the output when you run this code snippet?

nick woodward wrote:b1 = (b2 != !b2)
!= has the precedence, but again i'd argue that doesn't change the evaluation order, it just means that b2 is tied to =!b2, not b1=.
it just so happens that (b2 != !b2) is evaluated first because of right to left order of evaluation with assignments.
in fact !b2 within the subexpression should be evaluated first i think.

Why would you assume that !b2 should be evaluated first What about "(sub)expressions in Java are evaluated from left to right"? You have mentioned a few times in your post that "an expression is evaluated from left to right regardless of precedence". So why would this expression be evaluated differently?

Hope it helps!
Kind regards,
Roel
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


oh that is nasty.

well it's right to left with assignments, but in my head i want to evaluate array[++i] first - but am going to go with R-> L.

so i *100 = 0
array[1] = 0
output: [0,0]

Why would you assume that !b2 should be evaluated first

i don't know, that's what happens when I read someone's wrong answer. lets pretend i never said that. it doesn't make sense.

nick

*edit: i should've gone with my head and evaluated array[++i].
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:oh that is nasty.

You should know me by now ("nasty" would have been my middle name if it wasn't "roelinator" )

nick woodward wrote:i don't know, that's what happens when I read someone's wrong answer. lets pretend i never said that. it doesn't make sense.

Your wish is granted! (now I sound a bit like Aladdin's Genie )

nick woodward wrote:*edit: i should've gone with my head and evaluated array[++i].

You should have indeed! Probably because:
1/ expressions are evaluated from left-to-right
2/ the assignment operator is at the bottom of the precedence table and is evaluated from right-to-left, meaning the RHS (right hand side) will be assigned to the LHS (left hand side) and not the other way around.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic