INCREMENT AND
DECREMENT OPERATORS
C++ provides various different types of
Operators in order to compute mathematical
and logical statements and expressions in the
program. Increment Operator and Decrement
Operator are one such type of Unary
Operators in C++ which are used to add or
subtract the value of 1 from the operand
respectively.
Definition
Increment Operator is used to increase the value
of the operand by 1 whereas the Decrement
Operator is used to decrease the value of the
operand by 1.
In C++, the value of the variable is increased or
decreased by 1 with the help of the Increment
operator and the Decrement Operator
Increment and Decrement Operators are used only
with Integer Variables and Operands having
Numerical values. They cannot be used with a
Variable containing the values of a Character or
String.
Increment Operator
Syntax
Pre-Increment Operator:
++ variable name ;
Post-Increment Operator:
variable name ++ ;
Note 1
Both the Prefix and Postfix Increment operators
positions have the same functional values and
produce the same results if they are not used in any
expression format.
Example
#include<iostream>
Using name space std;
Int main()
{
Int x = 15 ; Int y = 30 ;
++x ;
y++ ;
cout << x << endl << y ;
}
Note 2
Both the Prefix and Postfix positions provide
different computational results if they are being
used in an expression.
If the Increment Operators are being used in the
Prefix position, then, the Increment function will
be done before the expression.
If the Increment Operators are being used in the
Postfix position, then, the Increment function will
be done after the evaluation of the expression.
Example
#include<iostream>
Using namespace std;
int main()
{
int x = 10 ;
int a ;
x = ++x ;
cout <<"Value of x = "<< x << endl ;
a = x++ ;
cout <<"Value of a = "<< a << endl ;
cout <<"New Value of x = "<< x << endl ; return 0;
}
Explanation:
In our First ‘cout’ statement, the Pre-Increment
Operator is used. Thus, the value of ‘x’ is
incremented by 1 i.e., x = x + 1 = 10 + 1 = 11
Then we assign value to the variable ‘a’ in the
expression ‘a = x++’
This means that First the value of ‘x’ will be
assigned to ‘a’ and then ‘x’ will be
incremented by 1. Hence the Output a =
11, new value of x = 12
Decrement Operator:
The main function of the Decrement Operator is
to decrease the numerical count of the variable
by a value of 1. In a programming language,
Decrement Operator is denoted by the sign ‘– –’.
Syntax
Pre-Increment Operator:
— — variable_name ;
Post-Increment Operator:
variable_name — — ;
Example:
#include<iostream>
Using namespace std;
int main()
{
int x = 15 ;
int y = 30 ;
-- -- x ;
y -- -- ;
cout << x << endl << y ;
}
Example 2:
int main()
{
int x = 10 ;
int a ;
cout <<"Value of x = "<< -- -- x << endl ;
a = x -- -- ;
cout <<"Value of a = "<< a << endl ;
cout <<"New Value of x = "<< x << endl ; return 0;
}
Output
Value of x = 9
Value of a = 9
New value of x = 8
Explanation:
In our First ‘cout’ statement, the Pre-
Decrement Operator is used. Thus, the value of
‘x’ will be decremented by 1 i.e., x = x – 1 = 10 –
1=9
Then we assign value to the variable ‘a’ in the
expression ‘a = x — —’
This means that First the value of ‘x’ will be
assigned to ‘a’ and then ‘x’ will be
decremented by 1. Hence the Output a =
9, new value of x = 8
Thank you