Skip to content
Related Articles
Open in App
Not now

Related Articles

C++ Increment and Decrement Operators

Improve Article
Save Article
  • Last Updated : 27 Nov, 2022
Improve Article
Save Article

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;
}


Output

Value 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;
}


Output

Value 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;
}


Output

Value 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;
}


Output

Value before using pre-decrement operator is : 5
The value stored by temp is : 4
After using the pre-decrement operator is : 4

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!