Increment:
Increment operator is this " ++ ". Its function is to add one to its operand. For example, following statements are same
- a++;
- a = a + 1;
- a += 1;
Decrement:
Decrement operator is " -- ". Its function is to subtract one from its operand or you can say that it is opposite of increment operator.
- a-- ;
- a = a - 1;
- a -=1;
Both increment operators can be use in prefix and postfix forms.In programming language, these prefix and postfix are two different things but their basic function is same either it is before incremented or decremented or after incremented and decremented depends upon the position of operator.
- Prefix form: First it increments its operand by one or changes its operand before taking its value.
++a; or --a; - Postfix form: First it takes its value and then increment its operand by one.
a++; or a--;
Prefix Form:
#include<iostream>
using namespace std;
int main()
{
int a=0,b=0;
b = ++a;
cout<<a<<" "<<b;
}
Output:
1 1
value of a = 1 , value of b = 1
Postfix Form:
#include<iostream>
using namespace std;
int main()
{
int a=0,b=0;
b = a++;
cout<<a<<" "<<b;
}
Output:
1 0
value of a = 1 , value of b = 0
No comments:
Post a Comment