-8

If you had the following code:

j=3;   // Line 1
i=6;   // Line 2
i+=5;  // Line 3
j=i--; // Line 4

... to my knowledge, the value of j would become 6. If it was --i, it would be 5 etc.

But if I print out the value of i after line 4, I get 10 instead of 11 which is on line 3.

Why does this happen? Does this mean if I declared i to be a number and later on I go back the code and set another variable to i--, before most of my i's in the code, it changes the value of i globally?

Chibuikem
  • 45
  • 5
  • "But if I print out the value of i after line 4 I get 10 instead of 11 which it is in line 3. Why does this happen?" Because line 4 decreases the value by 1. – Lundin Dec 16 '15 at 13:45
  • Possible duplicate of [How do the post increment (i++) and pre increment (++i) operators work in Java?](http://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java) – WalterM Dec 16 '15 at 13:47
  • 2
    @WalterM - It's best not to link as duplicates separate languages. – owacoder Dec 16 '15 at 13:52
  • 2
    @WalterM: That duplicate is a different language, and the nuances of the operator differ due to sequencing considerations. – Bathsheba Dec 16 '15 at 13:52
  • @Bathsheba My bad, I thought I was on Java tag. – WalterM Dec 16 '15 at 13:53
  • 1
    Here's the [C equivalent](http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i). Though I don't see how this is a duplicate. The op doesn't even seem to know what `i--` does to begin with. – Lundin Dec 16 '15 at 13:56
  • Ok. I see. I know what i-- does, but I didn't know it changed the value of all i after that. I just thought the new value would be set to j not to every i thereafter. Which was all I wanted to know in the first place. Thank you – Chibuikem Dec 16 '15 at 14:13
  • @WalterM if you suffer from that often, see your doctor:) – Martin James Dec 16 '15 at 15:54

3 Answers3

4

In your code, see the following step-through.

j=3;   //Line 1, j ==3
i=6;   //Line 2, i == 6
i+=5;  //Line 3, i == i + 5 == 11
j=i--; // line 4, j == 11, i == 10, after this line.

To elaborate, the x += y can be broken down as x = x + y, so that's it.

and regarding the post-decrement, the side-effect (decrement) will take place after the expression is evaluated. So, anyway, before the next statement, the value of i will get decremented.

To add some reference, from C11, chapter §6.5.2.4,

The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).[....]

and

The postfix -- operator is analogous to the postfix ++ operator, except that the value of the operand is decremented (that is, the value 1 of the appropriate type is subtracted from it).

Note, a difference of a pre-decrement and post-decrement is visible only within the expression they are used. From the perspective of the next instruction using the variable, they both will give you the same result (effect).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    @Downvoter, this looks correct to me. Presumably the answerer and I have missed something? – Bathsheba Dec 16 '15 at 13:42
  • @Bathsheba Please let me know if you get anything, probably i'm missing it too. :) – Sourav Ghosh Dec 16 '15 at 13:44
  • 1
    @Bathsheba I hate drive-by anonymous down-voters! Up-voted to restore the balance. –  Dec 16 '15 at 14:19
  • Maybe you should wear the Odinson hat you have? I think it would help. That hat looks a little bit like a police uniform, and some people have strong negative feelings against the police. Maybe that's why this got the down-vote. –  Dec 16 '15 at 14:20
  • 3
    @Ike While i should be happy (_actually I am_), but I will like to politely remind you, you should vote based on the content, not to restore balance. Thank you and appricicate it. :) – Sourav Ghosh Dec 16 '15 at 14:20
  • 2
    @SouravGhosh I am voting on the content -- but also to restore the balance. :-D Plus I also personally like your hat. –  Dec 16 '15 at 14:21
  • @Ike Thanks much. Cheers !! :) – Sourav Ghosh Dec 16 '15 at 14:22
  • My guess for why someone randomly down-voted your answer is because it's an answer to such a confusingly-worded question that got a lot of down-votes. Maybe they just didn't like that you chose to answer it. But your answer is fantastic and took the time to cite the language standard. It makes this otherwise poor question quite interesting. –  Dec 16 '15 at 14:33
  • Sadly I was not the answerer. – Bathsheba Dec 16 '15 at 14:35
  • @Bathsheba Ah yes, sorry, I edited it. I just wanted to ping you even though I knew you weren't the answerer about why this might have a down-vote. –  Dec 16 '15 at 14:35
2

First you make the value of i become 11, then you assign this value to j and then the decrement of i happens. The last line is equivalent to

j = i;
i -= 1;

If you did j = --i; then the last line would have been equivalent to

i -= 1;
j = i;

and j would have become 10.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

The -- decrement operators are not the same as subtraction by one. They modify the lvalue on which they were used, so any time i-- is used, i will be modified.

owacoder
  • 4,815
  • 20
  • 47