Increment decrement operator in C++ a++ ,a--,a+-,a-+
It is used to increase value of variable by adding one to the value of variable. For example we have variable a=3 and we want to increment its value by adding one to its original value then we use increment statement like this a++.
Syntax
int a=3;
a++;
So know the value of variable a will become 4.
Example 1
#include<iostream.h>
int main()
{
int a=5;
a++;
cout<<a;
system(“pause”);
return 0;
};
Output: 6
Example 2
#include<iostream.h>
int main()
{
int a=5;
a++;
a++;
cout<<a;
system(“pause”);
return 0;
};
Output: 7 Post increment: a++In post increment the cout statement is executed first and then variable is incremented. In this case the variable a is print first on the screen without any increment and then it is increment by 1.
int a=4;
Cout<<a++;
Output: 4 Example
int a=4;
Cout<<a++;
Cout<<a;
Output: 4,5
In this program the variable a is incremented in the step 2 but it does not show in step 2 it’s incremented value in the step 3 variable a show its incremented value.
Example1
#include<iostream.h>
int main()
{
int a=5;
a++;
cout<<a++;
system(“pause”);
return 0;
};
Output: 6
Pre increment: ++aIn pre increment the cout statement t and the value of variable is incremented at the same time. In this case the variable a is print on the screen with incremented value.
Int a=4;
Cout<<++a;
Output: 5
Example
Int a=4;
++a;
Cout<<++a;
Output: 6
Example
#include<iostream.h>
int main()
{
int a=5;
++a;
cout<<a++;
system(“pause”);
return 0;
};
Output: 6
Example
#include<iostream.h>
int main()
{
int a=5;
a++;
cout<<++a;
system(“pause”);
return 0;
};
Output: 7