Reputation: 1525
I am trying out the following Java snippet:
int[] testArray={10,20,30,40};
int i= 0;
testArray[i++]= testArray[i++]+1;
System.out.println("The value of i: "+i);
for(int i1=0;i1<testArray.length;i1++)
{
System.out.println(testArray[i1]);
}
With i=0, the output values of the array are: 21, 20,30,40
I can't understand this output because the output should be: 10, 11, 30, 40
testArray[0]+1
would be 11
and it would be then assigned to testArray[1]
but this is not the case.
Can anybody explain the output?
Upvotes: 4
Views: 4659
Reputation: 29
THIS:
This:
int i= 0;
testArray[i++]= testArray[i++]+1;
*** "And after this code executes, i == 2"
The thing about statements such as this is the order of operations.
[ ]
has higher precedence than ++
, such that testArray[i++]
means enter the array at index i, do whatever you came to do, then increment i.
And even though it serves no purpose for what you are wanting to accomplish, after everything completes on the right side of the assignment, the last thing to happen is that i gets incremented "again" : )
where initially, i=0
testArray[i++] = testArray[i++]+1;
for the expression on the left hand side:
testArray[0]
(which is the integer value 10), is where we enter the array (final result goes here!). Then, i gets incremented to 1
"equals"
for the expression on the right hand side:
using our newly incremented i variable....
testArray[1]
(which is the integer value 20) is added to the literal integer value 1, and the result (which is 21) is then stored in testArray[0].
And finally the i in the right side expression gets incremented to 2.
Or, we could just say:
i=0;
testArray[i] = testArray[i+1] + 1;
But honestly, where is the fun in that ?
Just for practice, code the same snippet using prefix notation on i and see what happens! Then read about "associativity" as it relates to order of operations.
Upvotes: -1
Reputation: 2896
The reason is because when you write the line
testArray[i++]= testArray[i++]+1;
You first access the array with
testArray[i++]
you access the first entry of the array, so testArray[0]. After this call i is increased to 1. When you then say
testArray[i++]+1
You get the value at testArray[1] (so 20) and add 1 to it.
Upvotes: 1
Reputation: 94429
This:
int i= 0;
testArray[i++]= testArray[i++]+1;
is equivalent to:
int i= 0;
testArray[0]= testArray[1]+1;
which is equivalent to:
int i= 0;
testArray[0]= 20 + 1;
The post increment operator is increasing the value of the int
causing it to pull the element in index 1 in the expression, that index is ==
20. Obviously, the subsequent addition will make the value 21, which is then assigned to index 0 of the array.
To put it another way that relates to your description of the code. Your not using the index of the array that you assume you are.
Upvotes: 2
Reputation: 95948
When you do:
testArray[i++] = testArray[i++] + 1;
Many things happens here.
test[i]
is evaluated i
gets incrementedtestArray[i]
(new value of i
) gets evaluated testArray[i]
testArray[i]
(again, new i
).What you probably want to do is:
testArray[i] = testArray[i++] + 1;
Upvotes: 1
Reputation: 213193
In the following assignment:
testArray[i++] = testArray[i++] + 1;
First the value of i++
is evaluated, which comes out to be 0
. Then the value of i
is incremented. So, the value of i
has become 1
before the RHS starts evaluating. So, basically, the above expression is equivalent to:
testArray[0] = testArray[1] + 1;
and after the above expression, the value of i
would be 2
. If you however change the expression to:
testArray[i] = testArray[i++] + 1;
.. you will get the expected result.
Upvotes: 5