Increment (++) and Decrement (–) Operator in C++
This tutorial is about the increment (++) and decrement (–) operator in C++. The increment operator increments the value of a variable while the decrement operator decrements the value of a variable by 1. Let’s discuss these operators in detail.
Increment Operator (++) in C++
The increment operator adds 1 to the value of a variable. There are two types of increment operator:
- Pre-increment operator
- Post-increment operator
The syntax for the increment operators is as follows:
a++ //post-increment operator
++a //pre-increment operator
Here a is a variable.
Pre-increment Operator
A pre-increment operator increments the value of a variable before using it in the given statement. See the given code for a better understanding.
#include <iostream>
using namespace std;
int main()
{
int a,b=1;
a=b;
cout<<"Value of b before increment operation is "<<b<<"."<<endl;
cout<<"Value of a before increment operation is "<<a<<"."<<endl;
//incrementing b and assigning its value to a
a=++b;
//printing value of a and b
cout<<"Value of a after increment operation is "<<a<<"."<<endl;
cout<<"Value of b after increment operation is "<<b<<"."<<endl;
return 0;
}Output:
Value of b before increment operation is 1. Value of a before increment operation is 1. Value of a after increment operation is 2. Value of b after increment operation is 2.
Post-increment Operator
A post-increment operator uses the current value of a variable in the given expression before incrementing it. See the example code.
#include <iostream>
using namespace std;
int main()
{
//declaring variable a and b
int a,b;
b=2;
a=b;
cout<<"Value of a before increment operation is "<<a<<"."<<endl;
cout<<"Value of b before increment operation is "<<b<<"."<<endl;
//post incrementing b and assigning its value to a
a=b++;
//printing a and b
cout<<"Value of a after increment operation is "<<a<<"."<<endl;
cout<<"Value of b after increment operation is "<<b<<"."<<endl;
return 0;
}Output:
Value of a before increment operation is 2. Value of b before increment operation is 2. Value of a after increment operation is 2. Value of b after increment operation is 3.
In the above code, the value of variable b is first stored in variable a and then b is incremented.
Decrement Operator (–) in C++
The decrement operator subtracts 1 from the value of a variable. There are two types of decrement operator:
- Pre-decrement operator
- Post-decrement operator
The syntax for the decrement operators is given here:
a– //post-decrement operator
–a // pre-decrement operator
Here a is a variable.
Pre-decrement Operator
A pre-decrement operator decrements the value of a variable before using it in the given statement. See the example code.
#include <iostream>
using namespace std;
int main()
{
int a,i=1;
a=i;
cout<<"Value of i before decrement operation is "<<i<<"."<<endl;
cout<<"Value of a before decrement operation is "<<a<<"."<<endl;
//decrementing i and assigning its value to a
a=--i;
//printing value of a and i
cout<<"Value of a after decrement operation is "<<a<<"."<<endl;
cout<<"Value of i after decrement operation is "<<i<<"."<<endl;
return 0;
}Output:
Value of i before decrement operation is 1. Value of a before decrement operation is 1. Value of a after decrement operation is 0. Value of i after decrement operation is 0.
Here, you can notice how i is decremented and then its value is assigned to variable a which is printed afterward.
Post-decrement Operator
A post-decrement operator uses the current value of a variable in the given expression before decrementing it. See the below code and explanation.
#include <iostream>
using namespace std;
int main()
{
int a,i=1;
a=i;
cout<<"Value of i before decrement operation is "<<i<<"."<<endl;
cout<<"Value of a before decrement operation is "<<a<<"."<<endl;
//decrementing i using post decrement operator
a=i--;
//printing values of a and i
cout<<"Value of a after decrement operation is "<<a<<"."<<endl;
cout<<"Value of i after decrement operation is "<<i<<"."<<endl;
return 0;
}Output:
Value of i before decrement operation is 1. Value of a before decrement operation is 1. Value of a after decrement operation is 1. Value of i after decrement operation is 0.
In the above code, the value of the variable i is first assigned to a and then decremented making the output differ from the previous example.
Thank you.
You may also read:
Leave a Reply