• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operators

 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Q6
{
public static void main(String[] args)
{
int a[]={4,5,9,8,6,6,0};
int i=1;
a[i]=i+=a[i++];
System.out.println(a[i]+" , "+i);
}
}
please explain the way output is coming?
0,6
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some body please come up with something.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i starts at 1
so a[1] gives you 5 (the second element of the array is 5)
i+=5 gives you 6
a[6] gives you 0 (the 7th element of the array is 0).
So the value of i ends up 6, and the valus of a[i] is 0.
 
Indika Perera
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i got it this way. Hope it's correct.
a[i]=i+=a[i++];
initially i = 1 so, expression become, a[1] = i+ = a[1]
Now, Evaluating from Right to left
first i+=a[1]; i = i + a[1] = 6
then a[1] = 6 Therefore a[6] remains 0 and i is now 6

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic