Can anyone explain me why such call doesn't increment my i value ?
int i = 0;
list.get(7 + (i = i++));
list.get(7 + (i = i++));
it leaves i=0 instead of increment by one at least such that in the second call it is 1.
i = i++ is like doing:
int old_i = i;
i = i + 1;
i = old_i;
What is actually happening is that the value of i++ is the value of i before the increment happens, then i will get the value of.. i.
In one line i++ will use the old value of i and then it will increment it.
i = i++ assigns first and increments second
heres what the execution essentially looks like:
list.get(7 + (i = i)); //list.get(7);
i = i + 1; //i = 1
list.get(7 + (i = i); //list.get(8);
i = i + 1; //i = 2
++i will increment the variable first and assign second
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With