1 2 Previous Next 15 Replies Latest reply: May 15, 2012 11:29 AM by Prem RSS

    evaluation of arithemetic expression

    Prem
      Hi everyone,

      Can somebody tell me the evaluation of following expression .

      int i=0;
      System.out.println( i++ + ++i + ++i);


      Please help me ..
        • 1. Re: evaluation of arithemetic expression
          796440
          Prem wrote:
          Hi everyone,

          Can somebody tell me the evaluation of following expression .

          int i=0;
          System.out.println( i++ + ++i + ++i);
          What do you think, and why?
          • 2. Re: evaluation of arithemetic expression
            Prem
            Actually I am not able to visualize how this expression is going to evaluate ?
            I mean the order of precedence . I know that stack is going to use in this case but not able to figure out how ?
            • 3. Re: evaluation of arithemetic expression
              796440
              Prem wrote:
              Actually I am not able to visualize how this expression is going to evaluate ?
              I mean the order of precedence
              Precedence doesn't really come into play here. What matters is that operands are evaluated left to right. So first we evaluate i++, and so on.
              • 4. Re: evaluation of arithemetic expression
                Prem
                Ok.. thanks for that knowledge..

                if operands are evaluated left to right then output of this expression should be 6 but it is 5 ? How ?
                • 5. Re: evaluation of arithemetic expression
                  796440
                  Prem wrote:
                  Ok.. thanks for that knowledge..

                  if operands are evaluated left to right then output of this expression should be 6 but it is 5 ? How ?
                  No, it should be exactly what it is. If that were not the case, it would mean that there is a HUGE error in a very basic part of Java that a beginner such as yourself was able to find, but that somehow didn't get fixed, despite Java being used in thousands of existing applications.

                  int i=0;
                  System.out.println( i++ + ++i + ++i); 
                  When we hit the first term (i++), the value of i is 0. The value of that expression is the original value of i, before incrementing, so we determine that the value of that expression is 0. Then we increment i to 1. So our running total is 0 and the value of i is 1.

                  When we hit the second term (++i), we first increment i from 1 to 2. Then we determine the value of that expression, which is the new value of i, which is 2. So we have 0 + 2 = 2 for our running total, and the value of i is now 2.

                  When we hit the third term (++i again), we first increment i from 2 to 3. Then we determine the value of that expression, which is the new value of i, which is 3. So we have 0 + 2 + 3 = 5 for our total, and the value of i is 3.

                  It's as if we did this:
                  int i = 0;
                  int j = i++; // j  = 0; i = 1;
                  int k = ++i; // i = 2; k = 2;
                  int m = ++i; // i = 3; m = 3;
                  System.out.println(j + k + m); // 0 + 2 + 3
                  Edited by: jverd on May 15, 2012 2:32 AM
                  • 6. Re: evaluation of arithemetic expression
                    Nitin Khare
                    The concept of prefix and postfix operators is used here. Output is 5 because the first postfix operator (i++) within SOP will first print the value of i which would be 0 and then it would be incremented to 1. Then the first prefix operator (++i) will make it 2 before adding it with 3 which is the value of i incremented by last prefix (++i) operator.
                    • 7. Re: evaluation of arithemetic expression
                      Prem
                      Thank you so much jevrd... It was really good example...

                      Thanks a lot and keep up this kind of help....
                      • 8. Re: evaluation of arithemetic expression
                        796440
                        Nitin Khare wrote:
                        The concept of prefix and postfix operators is used here. Output is 5 because the first postfix operator (i++) within SOP will first print the value of i which would be 0 and then it would be incremented
                        No, nothing is printed before i is incremented.
                        • 9. Re: evaluation of arithemetic expression
                          796440
                          Prem wrote:
                          Thank you so much jevrd... It was really good example...
                          You're very welcome. I'm glad you found it useful.
                          • 10. Re: evaluation of arithemetic expression
                            Prem
                            Hey Jeverd

                            One interesting thing

                            if suppose

                            i=0

                            i=i++;

                            sop(i)

                            it is printing 0 instead of 1
                            • 11. Re: evaluation of arithemetic expression
                              796440
                              Prem wrote:
                              Hey Jeverd

                              One interesting thing

                              if suppose

                              i=0

                              i=i++;

                              sop(i)

                              it is printing 0 instead of 1
                              Yup. Perfectly expected. And based on my explanation above, you should be able to figure out why.

                              Also, you, know that it's always wrong to write i = i++, right?
                              • 12. Re: evaluation of arithemetic expression
                                Prem
                                >

                                >
                                Yup. Perfectly expected. And based on my explanation above, you should be able to figure out why.

                                Also, you, know that it's always wrong to write i = i++, right?
                                Just for curiosity , I did i=i++

                                But in this case I guess i is pointing to some memory location . right ?
                                so in expression i=i++ , = has heigher precedence over postincrement operator so , i's current value which is 0 is assigned to i again in same memory location.
                                after that for i++ it go to same memory location and increment it by 1 .


                                Please correct me if I am wrong .
                                • 13. Re: evaluation of arithemetic expression
                                  796440
                                  Prem wrote:

                                  But in this case I guess i is pointing to some memory location . right ?
                                  The same location as itself? Yes, but that's not relevant.
                                  so in expression i=i++ , = has heigher precedence over postincrement operator
                                  No, it doesn't. If it did, then i = i++ would be equivalent to (i = i)++ which is illegal. The ++ operator requires a variable, and (i = i) is not a variable.

                                  The operation of i = i++; is:

                                  1. Evaluate the LHS to produce the variable i.

                                  2. Evaluate the i++ on the RHS.
                                  2.1 Value of the epxression i++ on the RHS is the current value of i, which is 0.
                                  2.2 Increment i from 0 to 1.

                                  3. Assign the value of the expression on the RHS (determined in #2) to the variable on the LHS. That is, stick the value 0 into variable i.

                                  If you did this:
                                  int i = 0;
                                  int j = i++;
                                  you would expect j to get the value 0, right? Because you know that j gets whatever the value of the expression on the RHS is, and you know that the value of the expression i++ is whatever i is before it gets incremented, right?

                                  Same thing is happening here.
                                  • 14. Re: evaluation of arithemetic expression
                                    Prem
                                    >
                                    The ++ operator requires a variable, and (i = i) is not a variable.
                                    So what if I place a code like this

                                    int i=0;
                                    i++;

                                    sop(i)


                                    so output would be 1 right ?
                                    1 2 Previous Next