• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Airthmetic extended operator - query

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from a mock exam :
public class q5 {
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]);
System.out.println (i);
}
}
The output is 0 and 6
---------------------------------------------------------------
a[i] = i += a[i++];
This will be resoved as a[i] = (i = (i + a[(i++)]))
Why is the array element a[i] not assigned the value of i (i.e 6)
Can any one explain me this
---------------------------------------------------------------



}
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mon,
we have,
a[i] = i+=a[i++]
first of all evaluate the given expression from left to right, so
a[1] = i+=a[1++]
now solve the RHS, like
i = (i) + a[1++];
i = 1 + a[1]; //as it is postfix here but as the value is being assigned to i itself, so the postfix op will be of no use,forget it.
now i=1+5
i=6;
u have System.out.println(a[i])and i is 6 so a[6]=0(as per array)
the value of i is 6 in the second statement.
HIH.
ashok.
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SO what happens to the post fix operation?

Originally posted by ashok khetan:
Mon,
we have,
a[i] = i+=a[i++]
first of all evaluate the given expression from left to right, so
a[1] = i+=a[1++]
now solve the RHS, like
i = (i) + a[1++];
i = 1 + a[1]; //as it is postfix here but as the value is being assigned to i itself, so the postfix op will be of no use,forget it.
now i=1+5
i=6;
u have System.out.println(a[i])and i is 6 so a[6]=0(as per array)
the value of i is 6 in the second statement.
HIH.
ashok.


 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roopa,
the postfix expression is kind of "discarded", it has no effect on the variable i, since i is stored where needed and then may not change anymore. Ashok's explanation is good, look carefully at it, you can see that it's is quite clear that i is post-incremented after a[i++] is evaluated but it has no effect since i is assigned a value right after the post-incrementation.
HIH
Val
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic