• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

chaining "="

 
Cedric Bosch
Ranch Hand
Posts: 92
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found 2 question in enthuware where the answer seem a bit contradictory at first glance, so I'd like some clarification.

First this :


a, b and c take the value of 9. So it looks like when chaining "=" it goes from right to left.

Second question :




Explanation of the answer :

Note that boolean operators have more precedence than =. (In fact, = has least precedence of all operators.)
so, in (b2 != b1 = !b2) first b2 != b1 is evaluated which returns a value 'false'. So the expression becomes false = !b2. And this is illegal because false is a value and not a variable!

Had it been something like (b2 = b1 != b2) then it is valid because it will boil down to : b2 = false.
Because all an if() needs is a boolean, now b1 != b2 returns false which is a boolean and as b2 = false is an expression and every expression has a return value (which is actually the Left Hand Side of the expression). Here, it returns false, which is again a boolean.

Note that return value of expression : i = 10 , where i is an int, is 10 (an int).



I'm not sure what I'm missing. Do they mean that the presence of the "!" operator make it so it's going left to right ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into operator precedence rules.
 
Henry Wong
author
Marshal
Pie
Posts: 22086
88
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
And make sure, when you are researching precedence tables, to use tables that also list associativity. As you already mentioned, assignments have right to left assoc, and relational operators have left to right assoc.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic