Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Please help to Understand x=++x+ + +x+x++

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Please help to Understand x=++x+ + +x+x++

    Hi Please help to understand this out put of this

    int x=10;
    x=++x+ + +x+x++; // i counted the output as 35. But correct out put 33. Pls help to understand.

    This is how i counted.

    ++x uses incremented value. So first ++x = 11
    next ++x increment x again. so second ++x = 12
    next x++ uses the current value in the memory. so uses 12. 11 + 12 + 12 = 35

    Thank you


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please help to Understand x=++x+ + +x+x++

    ...and what happened when you printed x on the following line?

    What do you get if you start with x++ then add x then add ++x?

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Please help to Understand x=++x+ + +x+x++

    Did you mean this?

    int x=10;
    x=++x+ + +x+x++;
    System.out.println(x); // output was 33. But i counted as 35.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please help to Understand x=++x+ + +x+x++

    pls see edited post above

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Please help to Understand x=++x+ + +x+x++

    Then i get 33 which is the correct answer.

    But for my calculation i get 35 for this x = ++x + ++x + x++; But the correct answer is 33. Im trying to find where im adding extra 2.

    This is how i calculated both expressions.

    x = ++x + ++x + x++;
    11 12 12 = 35

    x = x++ + x + ++x;
    10 11 12 = 33

  6. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Please help to Understand x=++x+ + +x+x++

    Note that '+ +' (with a space between the '+' symbols) is not the same as '++' (with no space between).

    That is, spaces between variables and operators are not significant, but the increment operator is '++' not '+ +'

    Evaluation goes from left to right.

    Try the following.

    The first print statement uses the expression as you posted it.

    For the second print statement I put parentheses around the three separate terms of the sum.

    For the third print statement, I eliminated the redundant unary '+' operators on the middle term.

    For the fourth print statement, I wrote the terms without parentheses. I kept the '++' operators close to the variables, the way that I usually write them. If the original statement had been like this would you have had a question about the result?

    For the fifth print statement, just for kicks, I separated the operators from variables with a space.


    public class Z {
        public static void main(String [] args) {
     
            int x;
     
            x = 10;
            x = ++x+ + +x+x++;
            System.out.println("1: x = " + x);
     
            x = 10;
            x = (++x) + (+ +x) + (x++);
            System.out.println("2: x = " + x);
     
            x = 10;
            x = (++x) + x + (x++);
            System.out.println("3: x = " + x);
     
            x = 10;
            x = ++x + x + x++;
            System.out.println("4: x = " + x);
     
            x = 10;
            x = ++ x + x + x ++;
            System.out.println("5: x = " + x);
        }
    }

    Output:

    1: x = 33
    2: x = 33
    3: x = 33
    4: x = 33
    5: x = 33


    Bottom line: Although this is a "cute" exercise that makes you think about things, I can't imagine a "real" situation in which I would write such confusing code. (Confusing, that is, to some of us mere humans, including the one who signs my paycheck, but not confusing to the compiler. The compiler never gets confused as long as the code has valid Java syntax.)

    Cheers!

    Z

  7. The Following 2 Users Say Thank You to Zaphod_b For This Useful Post:

    curmudgeon (December 2nd, 2012), rayan2004 (December 2nd, 2012)

  8. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Please help to Understand x=++x+ + +x+x++

    Wow. Thats really good explanation. I got it. Thank you for your help.

  9. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Please help to Understand x=++x+ + +x+x++

    Re
    int x=10; 
    x=++x+ + +x+x++;

    Have it be known however that anyone who writes code like that should be taken out back and shot. No exceptions.

  10. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Please help to Understand x=++x+ + +x+x++

    Quote Originally Posted by curmudgeon View Post
    Re
    int x=10; 
    x=++x+ + +x+x++;

    Have it be known however that anyone who writes code like that should be taken out back and shot. No exceptions.
    Yes, of course. Ahem. Except for the instructor who give this as an assignment...

Similar Threads

  1. Can someone help me understand toString()?
    By Psychotron in forum Java Theory & Questions
    Replies: 10
    Last Post: January 18th, 2012, 10:43 PM
  2. Help me to understand this
    By Madhushan in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2011, 08:47 AM
  3. Trying to understand what boolean[][][] is
    By JB4times4 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 29th, 2011, 05:03 PM
  4. I can''t understand this error
    By ragingdemon in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 8th, 2011, 06:07 PM
  5. 2 errors I can't understand
    By Brock in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 27th, 2010, 12:56 AM