Increment and Decrement Operators are the Unary Operators as they operate on a single operand. They are ++ and -- operators. ++ is used to increase the value by 1 and — is used to decrease the value by 1. There are two kinds of Increment and Decrement Operators.
- Post-Increment or Post-Decrement:
First, the value is used for operation and then incremented or decremented. Represented as a++ or a–. - Pre-Increment Pre-Decrement:
Here First the value is incremented or decremented then used for the operation. Represented as ++a or –a.
Let us understand the post and pre with an example.
Example: C++ program for Increment and Decrement Operators
#include <iostream>
using namespace std;
int main()
{
int a = 11;
int result;
//a value is not increased instantly, it will be increased after the assignment
result = a++;
cout << "Value of a++: " << result << endl; //prints 11
//now the value of is increased by 1
cout << "value of a: " << a << endl; //prints 12
//the value is increased before the assignment
result = ++a;
cout << "Value of ++a: " << result << endl; //prints 13
return 0;
}
Output:
Value of a++: 11
value of a: 12
Value of ++a: 13