SoFunction
Updated on 2025-03-03

C++ Implementation example of self-increase and self-decrease operator

The autoincrement operator ++ will add 1 to the operand, and the autoincrement operator – will subtract the operand by 1. therefore:

x = x+1;
Equivalent to
x++;

Same:

x = x-1;
Equivalent to
x--;

Whether it is the autoincrement operator or the autoincrement operator, it can be placed before (prefix) or after (suffix) of the operand. For example:

x = x+1;
Can be written as:
++x; // Prefix form

or:

x++; // Suffix form

There is a little difference between the prefix form and the suffix form. If the prefix form is used, the self-increment or self-decrease will be completed before the expression is calculated. If the suffix form is used, the self-increment or self-decrease will be completed after the expression is calculated.

Example

Please see the following examples to understand the difference between the two:

#include <iostream>
using namespace std;
int main()
{
   int a = 21;
   int c ;
   // The value of a will not increase itself before assignment   c = a++;   
   cout << "Line 1 - Value of a++ is :" << c << endl ;
   // After the expression is calculated, the value of a is increased by 1   cout << "Line 2 - Value of a is :" << a << endl ;
   // The value of a increases itself before assignment   c = ++a;  
   cout << "Line 3 - Value of ++a is  :" << c << endl ;
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Line 1 - Value of a++ is :21
Line 2 - Value of a is :22
Line 3 - Value of ++a is  :23

This is the end of this article about the implementation example of C++ self-increase and self-decrease operator. For more related contents of C++ self-increase and self-decrease operators, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!