wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes A question about i=i++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A question about i=i++" Watch "A question about i=i++" New topic
Author

A question about i=i++

zhu weitao
Greenhorn

Joined: Sep 02, 2008
Posts: 9
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

Joined: Mar 24, 2008
Posts: 3826

Please check this


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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

Joined: Jul 23, 2008
Posts: 352
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 ]

    be a well encapsulated person, don't expose your privates, unless you public void getWife()!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38481
        
      23
    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

    Joined: Oct 13, 2005
    Posts: 38481
        
      23
    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

    Joined: Jul 11, 2001
    Posts: 14112
    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


    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    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

    Joined: Oct 13, 2005
    Posts: 38481
        
      23
    You mean, " . . . b and c are added . . ." not, "a b and c," for my little example, presumably, Ilja.
    Bupjae Lee
    Ranch Hand

    Joined: May 14, 2007
    Posts: 107
    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

    Joined: Jul 11, 2001
    Posts: 14112
    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

    Joined: Oct 13, 2005
    Posts: 38481
        
      23
    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

    Joined: May 14, 2007
    Posts: 107
    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

    Joined: Oct 13, 2005
    Posts: 38481
        
      23
    In theory you should get the same result in C/C++, but some compilers can produce wildly differing result!
    Amit Ghorpade
    Bartender

    Joined: Jun 06, 2007
    Posts: 2716
        
        6

    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.


    SCJP, SCWCD.
    |Asking Good Questions|
    pradeepta chopra
    Ranch Hand

    Joined: Jul 05, 2008
    Posts: 137
    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

    Joined: Oct 27, 2005
    Posts: 19680
        
      18

    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.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Stephen Davies
    Ranch Hand

    Joined: Jul 23, 2008
    Posts: 352
    Cambell, thanks, for the correction, Im still getting my head round this, but as always, you and javaranch are indispensible! :roll:
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38481
        
      23
    You're welcome . . . we try our hardest and occasionally succeed
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: A question about i=i++