Results 1 to 5 of 5
  1. #1
    xtrmi is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default quick pre/post increment question

    int i5 = 7;
    System.out.println("5 #"+ (i5-- * (i5++ - ++i5)));

    I get -14 when I run this. So I'm wondering whats going on. Tell me if this is right.

    the first i5 is 7 then i5 is decremented to 6 the second i5 is 6 and is incrmented to 7 and the third i5 is incremented to 8 and then the math is executed. So this ends up being 7 * (6 - 8). I wonder if there is some other reasoning that would get me to -14 or my view is correct. Thanks in advance.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    The first i5 is 7, then it is decremented after it is evaluated, then the second is 6, and is incremented after it is evaluated, then the third i5 is 7 and is incremented before it is evaluated. Now, notice the italicised words and the postions of the incrementors (which is also the reason for their pre/post names).

    So, yes, your view is correct.

  3. #3
    xtrmi is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default

    Thanks! I was hoping that was right.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    For what it's worth (likely very little), I despise these trick increment questions. Whoever writes them should be taken out back and shot.

    Have a nice day.

  5. #5
    xtrmi is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

Similar Threads

  1. One last quick question
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 08:53 AM
  2. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 PM
  3. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 02:26 PM
  4. The first post and first question
    By m_kaleia in forum New To Java
    Replies: 8
    Last Post: 07-19-2008, 06:27 PM
  5. quick easy question
    By jmHoekst in forum New To Java
    Replies: 1
    Last Post: 06-19-2008, 03:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •