The increment (++) and decrement (--) operators in C++ are unary operators used to increase or decrease the value of a variable by 1. These operators are commonly used in loops, counters, and iterator operations.
++increases the value of a variable by 1.--decreases the value of a variable by 1.
Increment Operator in C++
The increment operator (++) increases the value stored in a variable by 1.
- It is a unary operator.
- It can be used only with modifiable numeric variables.
There are two types of increment operators:
Post-Increment operator (a++)
The postfix operator says that first use the value and then increment it. This means the value is first used for the operation and then increased by 1.
#include <iostream>
using namespace std;
int main()
{
int x = 5;
cout << "Value before using post increment operator is "
": "
<< x << endl;
int temp = x++;
cout << "The value stored by temp is : " << temp
<< endl;
cout << "After using the post increment operator is : "
<< x << endl;
return 0;
}
Output
Value before using post increment operator is : 5 The value stored by temp is : 5 After using the post increment operator is : 6
Explanation: The value of x is first assigned to temp, after which x is incremented by 1.
Pre-increment operator(++a)
The prefix operator says that first increment the value and then use it. This means the value is increased by 1 before it is used in the expression.
#include <iostream>
using namespace std;
int main()
{
int x = 5;
cout
<< "Value before using pre increment operator is : "
<< x << endl;
int temp = ++x;
cout << "The value stored by temp is : " << temp
<< endl;
cout << "After using the post increment operator is : "
<< x << endl;
return 0;
}
Output
Value before using pre increment operator is : 5 The value stored by temp is : 6 After using the post increment operator is : 6
Explanation: The value of x is incremented first and then assigned to temp.
Decrement Operator in C++
The decrement operator (--) decreases the value stored in a variable by 1.
- It is a unary operator.
- It can be used only with modifiable numeric variables.
There are two types of decrement Operator:
Post-decrement operator (a--)
The postfix operator says that first use the value and then decrement it. This means the value is first used for the operation and then decreased by 1.
#include <iostream>
using namespace std;
int main()
{
int x = 5;
cout << "Value before using post decrement operator is "
": "
<< x << endl;
int temp = x--;
cout << "The value stored by temp is : " << temp
<< endl;
cout << "After using the post decrement operator is : "
<< x << endl;
return 0;
}
Output
Value before using post decrement operator is : 5 The value stored by temp is : 5 After using the post decrement operator is : 4
Explanation: The current value of x is assigned to temp, after which x is decreased by 1.
Pre-decrement operator (--a)
The prefix operator says that first decrement the value and then use it. This means the value is decreased by 1 before it is used in the expression.
#include <iostream>
using namespace std;
int main()
{
int x = 5;
cout
<< "Value before using pre-decrement operator is : "
<< x << endl;
int temp = --x;
cout << "The value stored by temp is : " << temp
<< endl;
cout << "After using the pre-decrement operator is : "
<< x << endl;
return 0;
}
Output
Value before using pre-decrement operator is : 5 The value stored by temp is : 4 After using the pre-decrement operator is : 4
Explanation: The value of x is decremented first and then assigned to temp.
Pre Vs Post Operators
| Operator | Behavior |
|---|---|
a++ | Use the current value, then increment |
++a | Increment first, then use the updated value |
a-- | Use the current value, then decrement |
--a | Decrement first, then use the updated value |