Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about i=i++

 
zhu weitao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
give:



I know the answer is:0
but I wana know why?

thanks!

[edit]Add code tags. CR[/edit]
[ September 02, 2008: Message edited by: Campbell Ritchie ]
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check this
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please find the Code Button; I shall edit your post so you can see how much easier it would be to read.

This is a classic pitfall which everybody falls for; if you do a search of JavaRanch you will find it comes up several times a year.

There are two values you need to consider; the value of the variable i and the value of the expression i++. When you start off, the value of i is 0, but the value of i++ is the old value of i, still 0.
You start going into the i = i++; statement; the first things that happen are that i++ is evaluated (as stated previously that is 0) and i is incremented, so it is now 1.

Then what happens is that i is set to the value of whatever is to the right of the =, which we have already seen is 0. The value 1 vanishes into cyber-limbo never to be seen again.

Try changing that statement to i = ++i; and see what happens.
[ September 02, 2008: Message edited by: Campbell Ritchie ]
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell is of course right, but if it helps here is a simplistic take on my understanding (i=i+1) or (i++)

  • A: in post incrementation / decrementation (n++/--)the direction is; assignment >>> incrementation / decrementation.
  • B: in pre incrementation / decrementation the direction is; incrementation / decrementation (++n/--n)>>> assignment.


  • So in assignment your variable is simply assigned as usual, for example.

    Sudo Code



    hope this helps!



    :roll:

    [ September 02, 2008: Message edited by: Stephen Davies ]
    [ September 02, 2008: Message edited by: Stephen Davies ]
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50217
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Stephen Davies:
    . . . here is a simplistic take on my understanding (i=i+1) or (i++)

  • A: in post incrementation / decrementation (n++/--)the direction is; assignment >>> incrementation / decrementation.
  • B: in pre incrementation / decrementation the direction is; incrementation / decrementation (++n/--n)>>> assignment.

  • No it isn't. The ++ operators have a much higher priority than assignment. You have to get used to doing it the hard way.

    If you use i++ you get the old value of i.
    If you use ++i you get the new value of i.

    A lot of people find this confusing; if you go through the Ranch and find the "Chicken Coop" style guide you find a possible way to avoid that particular confusion.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50217
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by myself:
    You have to get used to doing it the hard way.


    What is going to happen if you have something like this?
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Campbell Ritchie:


    What is going to happen if you have something like this?


    Roughly, in order:

    - the value of b is remembered
    - the value of c is remembered
    - the value of c is increased
    - the remembered values of a b and c are summed up
    - the sum is assigned to a
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Stephen Davies:
    Campbell is of course right, but if it helps here is a simplistic take on my understanding (i=i+1) or (i++)

  • A: in post incrementation / decrementation (n++/--)the direction is; assignment >>> incrementation / decrementation.
  • B: in pre incrementation / decrementation the direction is; incrementation / decrementation (++n/--n)>>> assignment.


  • So in assignment your variable is simply assigned as usual, for example.

    That simplification couldn't explain the behavior of

    i = i++ + i++;

    as far as I can tell. (which is quite academical, of course)
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50217
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You mean, " . . . b and c are added . . ." not, "a b and c," for my little example, presumably, Ilja.
     
    Bupjae Lee
    Ranch Hand
    Posts: 107
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In my opinion, the "correct" way to explain is using "operation stack" (actually JVM does)

    In the case of "i = i++ + i++"

    1) value of i is pushed on the stack
    2) value of i is increased
    3) value of i is pushed on the stack
    4) value of i is increased
    5) pop two value from stack, add them, and push
    6) pop value and assign to i

    Below is the code which javac translates "i=i++ + i++":



    ps1) In the C/C++, the result is UNDEFINED.
    ps2) It is strongly recommand that one variable should not be changed more than once in one sentence.
    ps3) Unfortunely, some professors in college/university like these kind of quiz.
    [ September 03, 2008: Message edited by: Bupjae Lee ]
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Campbell Ritchie:
    You mean, " . . . b and c are added . . ." not, "a b and c," for my little example, presumably, Ilja.


    Not sure what you are talking about...
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50217
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ilja: I think it was a little misprint in a previous post; you said "a b and c" were summed; in the snippet I posted it said "a = b + c++;".

    I find the stack you posted, Bupjae Lee, difficult to understand, but I can see how starting from (int i = 1;) you get the correct result of 3.

    The reason people like that sort of "quiz" is to make people think. In this country it is actually less common at colleges, but similar questions come up on JavaRanch often enough, and might come up in certification exams, so people need to understand them.

    [edit]Disable smilies[/edit]
    [ September 04, 2008: Message edited by: Campbell Ritchie ]
     
    Bupjae Lee
    Ranch Hand
    Posts: 107
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The reason people like that sort of "quiz" is to make people think.


    When quiz like "i = i++ + i++" is provided in java language class, answer is one, thought confusing.

    However, there are some professors who make same quiz in C/C++ language class, which is UNDEFINED under C/C++ specification, and I wanted to metion this point.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50217
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In theory you should get the same result in C/C++, but some compilers can produce wildly differing result!
     
    Amit Ghorpade
    Bartender
    Posts: 2854
    10
    Fedora Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Campbell Ritchie:
    In theory you should get the same result in C/C++, but some compilers can produce wildly differing result!

    You guessed it right, even same compiler on different machines seem to be overriding the precedence of operators set by the C++ language semantics.
     
    pradeepta chopra
    Ranch Hand
    Posts: 137
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    seems like in case like this
    int i=0;
    i=i++ + i++ + i++;
    System.out.println(i);

    1.the value of first i++ remains that of i which is 0 only as explained above.
    2.the next i++ increments the value of previous i++ that is 0+1=1 and so on.
    3. hence the result is 3.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20665
    65
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In Java yes. In C / C++ this behaviour is simply undefined by the language specification, and therefore can return 0 if the compiler developers chose to evaluate everything first, then increment i three times.
     
    Stephen Davies
    Ranch Hand
    Posts: 352
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Cambell, thanks, for the correction, Im still getting my head round this, but as always, you and javaranch are indispensible! :roll:
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50217
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome . . . we try our hardest and occasionally succeed
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic