Tuesday, 14 July 2015

Difference b/w Prefix and Postfix Increment :


When increment operator is used independently, prefix and postfix form work similarly. For example, the result of A++ and ++A is same.

 But when increment operator is used in a larger expression with other operators, prefix and postfix forms work differently. For example, the results of two statements A = ++B and A = B++ are different.


In prefix form, The statement A = ++B contains two operators ++ and =. It works in following order:

  1. It increments the value of B by 1.
  2. It assigns the value of B to A.

In postfix form, the statement A = B++ works in the following order :

  1. It assigns the value of B to A.
  2. It increments the value of B by 1.



No comments:

Post a Comment