Increment & Decrement Operators In C++
The operators that is used to add 1 of the value of a variable is called increment operator.Similarly, the operator that is used to subtract 1 from the value of the variable is called decrement operator.
The Increment Operator (++):
The increment operator is represented by a double (++) sign. it is used to add 1 to the value of an integer variable. This operator can be used before or after the variable name.
For example, to add 1 to a value of variable xy, it is normally written as
xy=xy+1;
By using increment operator "++" it is written as
xy++;
Prefix Increment Operator:
When an increment operator is used in prefix mode in an expression , it adds 1 to the value of the variable before the value of the variable is used in the expression . For example
# include<iostream.h>
main()
{
int,a,b,c,sum;
a=1;
b=1
c=3;
sum=a+b+(++c);
cout<<"sum="<<sum;
cout<<"c ="<<c;
}
In the above program 1 will be added to the value of c before it is used in an expression. Thus after execution , the sum will be equal to 6 and value of c will be 4.
Postfix Increment Operator:
When an increment operator is used in postfix mode in an expression, it adds 1 to the value of the variable after the value of the variable has been used in the expression.
For example:
sum=a+b+c++;
For example:
sum=a+b+c++;
The Decrement Operator (- -)
The decrement operator is represented by a double minus - - sign. It is used to subtract 1 from the value of an integer variable.
For example to subtract 1 from the value of a variable xy, the decrement statement is written as:
xy--; or --xy;