0 votes
in C Plus Plus by

State the difference between Pre and Post Increment/Decrement Operations.

1 Answer

0 votes
by
C++ allows two operators i.e ++ (increment) and –(decrement), that allow you to add 1 to the existing value of a variable and subtract 1 from the variable respectively. These operators are in turn, called increment (++) and decrement (–).

Example:

a=5;

a++;

The second statement, a++, will cause 1 to be added to the value of a. Thus a++ is equivalent to

a = a+1; or

a += 1;

A unique feature of these operators is that we can prefix or suffix these operators with the variable. Hence, if a is a variable and we prefix the increment operator it will be

++a;

This is called Pre-increment. Similarly, we have pre-decrement as well.

If we prefix the variable a with an increment operator, we will have,

a++;

This is the post-increment. Likewise, we have post-decrement too.

The difference between the meaning of pre and post depends upon how the expression is evaluated and the result is stored.

In the case of the pre-increment/decrement operator, the increment/decrement operation is carried out first and then the result passed to an lvalue. Whereas for post-increment/decrement operations, the lvalue is evaluated first and then increment/decrement is performed accordingly.

Example:

a = 5; b=6;

++a;       #a=6

b–;         #b=6

–a;         #a=5

b++;      #6

Related questions

0 votes
asked May 14, 2020 in DevOps by sharadyadav1986
0 votes
asked Dec 3, 2019 in SOAPUI by Sinaya
...