Program output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vskulkarni
    New Member
    • Sep 2007
    • 1

    #1

    Program output

    the core code below produces out put '0' and not '1'
    why..?

    class Test {
    public static void main(String [] args) {

    int i=0;
    i+= i++;
    System.out.prin tln("i = "+i);
    }
    }

    reply..
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by vskulkarni
    the core code below produces out put '0' and not '1'
    why..?

    class Test {
    public static void main(String [] args) {

    int i=0;
    i+= i++;
    System.out.prin tln("i = "+i);
    }
    }

    reply..
    Google post increment and read a tutorial about it.
    Last edited by r035198x; Sep 19 '07, 08:22 AM. Reason: increment, increment, increment, increment, increment, increment

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by vskulkarni
      the core code below produces out put '0' and not '1'
      why..?

      class Test {
      public static void main(String [] args) {

      int i=0;
      i+= i++;
      System.out.prin tln("i = "+i);
      }
      }

      reply..
      The reason is, that i++ becomes active after reading the value of a, but before the adding is done. So what it does is:
      [CODE=java]
      int i = 0;
      int tmp = i; // tmp = 0;
      i++; // i = 1;
      i = tmp; // i = 0;
      [/CODE]Quite confusing, isn't it?

      Greetings,
      Nepomuk

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by r035198x
        Google post incremenent and read a tutorial about it.
        Google's output:

        Originally posted by Google

        Results 1 - 10 of about 272 for incremenent. (0.28 seconds)
        Did you mean: increment
        kind regards,

        Jos :-P

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by JosAH
          Google's output:



          kind regards,

          Jos :-P
          ... and we've just run out of Rooibos tea ...

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by vskulkarni
            the core code below produces out put '0' and not '1'
            why..?
            [code=java]
            class Test {
            public static void main(String [] args) {

            int i=0;
            i+= i++;
            System.out.prin tln("i = "+i);
            }
            }
            [/code]

            reply..
            Welcome to TSDN.
            Use code tags.

            Look here i=0 and i++ produces 0 so the result is 0 here.

            Kind regards,
            Dmjpro.

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by r035198x
              ... and we've just run out of Rooibos tea ...
              Oh no! Will you survive? Quick, everyone, send your Rooibos tea to r035198x by express delivery! This might end fatal otherwise...

              ;-)
              Nepomuk

              Comment

              Working...