• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

array increment

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

public class Test3{
public static void main(String args[]){
int arr[] = new int[10];
int i = 5;
arr[i++] = ++i+i++;
System.out.print(arr[5]+":"+arr[6]);
}
}


when i try to do this program I am getting the output as 11:13 but the compiler shows as 14:0.

can any please expalin me the program.

Thanks in advance.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The array reference on the left hand side of the assignment is evaluated first, to arr[5]. After this evaluation, i is incremented to 6.

Then the right hand is evaluated. First i is incremented to 7. This value is remembered, and then the "i++" is evaluated, giving 7. This second 7 is remembered, and then then i is incremented again, and i becomes 8. This value 8 is never used for anything, as i is never referenced again. The two remembered sevens are then added to give 14, which is assigned to the location arr[5]. That's it!
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by prerna boja:
Hi,

public class Test3{
public static void main(String args[]){
int arr[] = new int[10];
int i = 5;
arr[i++] = ++i+i++;
System.out.print(arr[5]+":"+arr[6]);
}
}


when i try to do this program I am getting the output as 11:13 but the compiler shows as 14:0.

can any please expalin me the program.

Thanks in advance.



Where are you getting te output 11:13?
There is only one array variable being set and that happens to be index 5.
the line
arr[i++] = ++i+i++;
comes out to be (let's see if I can get this right)
arr[5] = 7 + 7;
IF I'm right it happens a such:
first the array access happens at 5 and then increments i to 6
the pre increment ups i to 7 giving you 7 + 7
then the post increment ups i to 8, but that doesn't get used.
I'm pretty sure that if you added a println(i) at the end you would see 8.
 
prerna boja
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thank you I got it,but still I have doubt

if i write the same program as

arr[i++] = i++ + ++i;----------------- 1

instead of arr[i++] = ++i + i++;---------------- 2

the out put is same ,but

when i try to solve the 1 equation like

see i=5
so arr[i++]= arr[5] (post increment then i=6)
so now i++ = 6 (ost increemnt then i=7)
then ++i= 7.

so arr[5] = 6 + 7=13 (is this correct?)

but i know that both 1 and 2 are one and the same .

still not getting satified as i feel the outputs for both of them is different.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's not 6 + 7, it's still 7 + 7 because the ++i happens before the +
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's 6 + 8.
i++ returns 6, increments to 7
++i increments to 8, returns 8
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm seeing it as:
arr[i++] = ++i + i++; //7 + 7
//i = 8 now
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vlado Zajac:
It's 6 + 8.
i++ returns 6, increments to 7
++i increments to 8, returns 8


If you look back at the code, i was already incremented to 7 in the subscript. So it's exactly like PJ and others say: 7+7 with i==8 in the end.

Layne
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic