C++ Increment and Decrement Operators
Last Updated :
27 Nov, 2022
Prerequisite: Operators in C++
What is a C++ increment Operator?
The C++ increment operator is a unary operator. The symbol used to represent the increment operator is (++). The increment operator increases the value stored by the variable by 1. This operator is used for Numeric values only.
There are two types of C++ increment Operator:
- Pre-Increment
- Post-Increment
1. Post-Increment operator (a++)
The postfix operator says that first use the value and then increment it. This means the value is first used up for the operation then the value is updated by 1.
Example:
C++
// C++ Program to implement
// Post-Increment
#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;
}
OutputValue before using post increment operator is : 5
The value stored by temp is : 5
After using the post increment operator is : 6
2. Pre-increment operator(++a)
The postfix operator says that first increment the value then use it. This means the value is increased by 1 for the operation then the value is used by the variable
Example:
C++
// C++ Program to implement
// Pre-Increment
#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;
}
OutputValue before using pre increment operator is : 5
The value stored by temp is : 6
After using the post increment operator is : 6
What is a C++ decrement Operator?
The C++ decrement operator is a unary operator. The symbol used to represent the increment operator is (--). The decrement operator decreases the value stored by the variable by 1. This operator is used for Numeric values only.
There are two types of C++ decrement Operator:
- Post-decrement operator
- Pre-decrement operator
1. Post-decrement operator (a--):
The postfix operator says that first use the value and then decrease it. This means the value is first used up for the operation then the value is decreased by 1.
Example:
C++
// C++ Program to implement
// Post-Decrement
#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;
}
OutputValue before using post decrement operator is : 5
The value stored by temp is : 5
After using the post decrement operator is : 4
2. Pre-decrement operator (--a)
The postfix operator says that first decrease the value and then use it. This means the value is decreased by 1 for the operation then the value is used by the variable
Example:
C++
// C++ Program to implement
// Pre-Decrement
#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;
}
OutputValue before using pre-decrement operator is : 5
The value stored by temp is : 4
After using the pre-decrement operator is : 4
Similar Reads
Increment and Decrement Operators in C The increment ( ++ ) and decrement ( -- ) operators in C are unary operators for incrementing and decrementing the numeric values by 1, respectively. They are one of the most frequently used operators in programming for looping, array traversal, pointer arithmetic, and many more.Increment Operator i
4 min read
Increment (++) and Decrement (--) Operator Overloading in C++ Operator overloading is a feature in object-oriented programming which allows a programmer to redefine a built-in operator to work with user-defined data types. Why Operator Overloading? Let's say we have defined a class Integer for handling operations on integers. We can have functions add(), subtr
4 min read
Increment and Decrement Operators in Programming Increment and Decrement Operators are Unary Operators commonly used in programming to increase or decrease the value of a variable by one, respectively. They provide a shorthand way to perform these common operations. Table of Content Increment OperatorsIncrement Operators in CIncrement Operators in
7 min read
Pre-increment and Post-increment in C/C++ In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol. The increment operator can be classified into two types:Pre-Increment OperatorPost-Increment OperatorPre and Post Increment Operator in C/C++ The pre and post-increment ope
4 min read
C# Program to Overload Unary Increment (++) and Decrement (--) Operators In C#, overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement method overloading by defining two or more methods in a class sharing the same name but with different method signatures. So in this article, we wil
3 min read
Pre Increment and Post Increment Operator in Programming Pre Increment Operator and Post Increment Operator are the two ways of using the Increment operator to increment the value of a variable by 1. They can be used with numeric data values such as int, float, double, etc. Pre-increment and Post-increment perform similar tasks with minor distinctions. In
6 min read