Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about multiple assignments

 
Catherine Fogg
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone enlighten me about the behaviour of multiple assignments? Given the following code:
int i = 0;
int[] a = { 3, 6 };
a[i] = i = 9;
I would have expected that the last line would evaluate as follows:
(a[i] = (i = 9))
a[i] = 9
a[9] = 9
= ArrayIndexOutOfBoundsException
However, it evaluates to:
a[0] = 9
Can anyone explain?
 
Eureka Jana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yah what you expected is correct, but it is not like that.See the operators precedence.
Higher :[]
Lower: =
So first a[i] gets evaluated and the assignment from right to left.
I would like to convey one message to all.Since we are all beginners, the basic thing in learning any language is to make sure we are well aware of "Operators Precedence".That solves many problems.
Thanks,
Jana
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to take a look at last week's conversation on the assignment operator.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic