It is used to decrease the value of variable by 1 . For example we have variable a=3 and we want to decrement its value by 1 then we use decrement Operator.
int a=3; a--;
So know the value of variable a will become 2.
Example 1
#include<iostream.h> int main() { int a=5; a--; cout<<a; system(“pause”); return 0; };Output: 4
Example 2
#include<iostream.h> int main() { int a=5; a--; a--; cout<<a; system(“pause”); return 0; };Output: 3
Post decrement: a--
In post decrement the cout statement is executed first and then variable is decremented. In this case the variable a is print first on the screen without any decrement in its value and then it is decrement by 1.
int a=4; Cout<<a--;
Output: 4
Example 1
int a=4; cout<<a--; cout<<a;Output: 4,3
Example
#include<iostream.h> int main() { int a=5; a--; cout<<a--; system(“pause”); return 0; };Output: 4
Pre decrement: --a
In pre decrement the cout statement t and the value of variable is decremented at the same time. In this case the variable a is print on the screen with its decremented value.
int a=4; cout<<--a;Output: 3
Example
int a=4; --a; Cout<<--a;Output: 2
Example
#include<iostream.h> int main() { int a=5; --a; cout<<a--; system(“pause”); return 0; };Output: 4
Example
#include<iostream.h> int main() { int a=5; a--; cout<<--a; system(“pause”); return 0; };Output: 3