Difference between pre-increment and post-increment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinoth102
    New Member
    • Nov 2013
    • 3

    #16
    x=5, y=5
    x=y++ //Ans: x=5, y=6


    x=5, y=6
    x=++y; //Ans: x=6, y=6

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #17
      @vinoth102 The initial condition for your second example should be y=5.

      Comment

      • Sherin
        New Member
        • Jan 2020
        • 77

        #18
        Pre-increment

        Pre increment operator is used to increment variable value by 1 before assigning the value to the variable.


        Syntax:

        a = ++x;


        Post-increment

        Post increment operator is used to increment variable value by 1 after assigning the value to the variable.


        Syntax:

        a = x++;

        Comment

        • Naheedmir
          New Member
          • Jul 2020
          • 62

          #19
          ++X is the prefix increment operator; this means that the value of x is first incremented and then used in the program. X++ is a postfix increment operator; this means that the value of x is first used in the program and then incremented. --x is prefixed decrement operator, this means that the value of x is first decremented and then used in the program. X-- is a postfix decrement operator, this means that the value of x is first used in the program and then decremented.

          Comment

          • AjayGohil
            New Member
            • Apr 2019
            • 83

            #20
            Hi,

            Preincrement take value and then increment.Post increment first increment and then take value

            you can understand easily from below example.

            open browser and run following piece of code in console.

            Code:
            var x=5;
            //out put:undefined
            var pre=++x;
            //output:undefined
            pre
            //output:6
            x
            //output:6
            var post=x++;
            //output:undefined
            post
            //output:6

            Comment

            Working...