• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Precedence of operators

 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

what is the difference between left to right associative and right to left associative with eg:


thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Left to Right means that the evaluation is done left to right. For example, 2+3+4 -- obviously, Java can't do both additions at the same time, and they have the same precedence. It is however, left to right, so the left addition is done first.

Henry
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . And right to left means that if you have

. . . myBoolean ? 123 : otherBoolean ? 345 : 567 . . .

where both the ?: operators have the same priority but associate to the right, the compiler evaluates the bit on the right (345 or 657) before offering that result as an alternative to the bit on the left (123).
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic