English

# Increment / Decrement Operators in C++

What are Increment / Decrement Operators in C++?
How to add or subtract an integer variable in C++?

## Explanation

The increment and decrement operator are used to increase or decrease the value of an operand by "1" or simply, to add or subtract integer variable. These operators can be used add or subtract an operand before the operation or after the operation, which is known as post increment or pre increment of integer variable and vice versa.

#### Example :

 #include using namespace std;void main( ){int i = 3, k,l, n=6,m;//Example for post and pre incrementk = i++;l = ++i;cout << "Post Increment value for 3 is::" << k << endl;cout << "Pre Increment value for 4 is::" << l << endl;//Example for post and pre decrementm=--n;cout << "Pre Decrement value of 6 is::" << m << endl;for( int x = 1; x > 0;x-- ){cout << "Value of x before Post Decrement::" << x << "\n";x--;cout << "Value of x after Post Decrement::" << x << "\n";}}

Result :

Post Increment value for 3 is::4
Pre Increment value of 4 is::5
Pre Decrement value of 6 is::5
Value of x before Post Decrement::1
Value of x after Post Decrement::0

In the above example the difference is that k will be assigned 3 first then its post-incremented to become "4" . In the next scenario "k" already has the pre-incremented value of i that is "5".The pre-decrement operator is used to decrement the value of n even before assignment.Using the post-decrement operator the value of "x" is decremented after the assignment using a loop.

#### Example :

 #include using namespace std;void main( ){for( int x = 1; x > 0;x-- ){cout << "Post decrement value of x is::" << x << "\n";}}

Result :

Post Decrement value of x is::5
Post Decrement value of x is::4
Post Decrement value of x is::3
Post Decrement value of x is::2
Post Decrement value of x is::1

In the the above example the given value is post-decremented after the assigned value of "5".