Pages

Post increment vs pre increment

Post and pre-increment  can confuse many programmers . The best way to know the difference betwenn two is an example .

           Case1 :
                           var x = 10 ;
                           var y = x++ ;


What is value of y while it is being equated to x++ ?

            Case 2 :

                            var x = 10;
                            var y = ++x;


What is value of y while it is being equated to ++x ?

Answers :
Case 1 : 10
Case 2 : 11

Though note that when we move to the next statement value of both the variables will be 11 . Values vary only in case while the statement is being executed . In case there is no comparison operator values are same in both cases .