Unary Arithmetic Operators
These operators require only one operand to perform
operation .it can again divided into Two categories
They are
1.
Unary
minus Operator
2. Increment/ Decrement operators
Unary minus or negation operator
This operator can be used to negate the value of a
variable i.e it converts positive value into negative and vice versa.
For example
int a= 10; -------> Line
1
int b=
-a; --------> Line
2
In the above Example Line 1 a will be stored with
the value 10(Positive value), and in Line 2 int b= -a the value of a(10) is converted into
negative i.e -10 and stored into the
variable b.
Note: Minus operator can be used in both
unary and binary operations.
Increment /Decrement operators
These unary operators are used to increase or decrease
the value of variable by 1.increment operators are represented by ++ and decrement
operators are represented by --.These operators can be used in two ways
1. Pre-fix
operator
2. Post-fix
operator
Pre-fix operator:
If the operator is used before the variable name then the operator is known as
prefix operator
For Example
++a ;
--a ;
In the above example ++ and --are prefixes to the
variable a. hence ++ is known as pre-increment and - - is known as pre -
decrement.
Post-fix operator:
If the operator is used after the variable name then the operator is known as
post-fix operator
For Example
a++;
a--;
In the above example ++ and --are post-fixes to the
variable a. Hence ++ is known as post-increment and - - is known as post -
decrement.
Pre-Increment Operator:
Here first
it increases the value of the variable by 1.and then the new value in the
variable will be used in the expression
For example
Int a =10;
Int b;
b=++a; then a=?
and b= ?
in the above example value of a is incremented by 1
first i.e a becomes 11 and then the value of a(11) will be assigned to b .
There fore
a=11
b=11
Pre-decrement Operator:
Here first
it decreases the value of the variable by 1.and then the new value in the
variable will be used in the expression
For example
Int a =10;
Int b;
b=--a; then a= ? and b= ?
in the above example value of a is incremented by 1
first i.e a becomes 11 and then the value of a(11) will be assigned to b .
There fore
a=9
b=9
Post Increment operator
post Increment operator:
if the operator is post increment
operator then the value of variable is used in the expression first and
then the value of the variable increments next.
For example:
Int a=10;
Int b;
b=a++;
Here ++ is post-increment operator, since the value
of the variable a(10) is assigned to
b is first and then value of a is incremented by 1 .i.e. a becomes 11.There fore
a=11;
b=10;
Post decrement operator: if the operator is
post decrement operator then the value of variable is used in the expression
first and then the value of the variable increments next.
For example:
Int a=10;
Int b;
b=a--;
Here -- is post-increment operator, since the value
of the variable a (10) is assigned
to b is first and then value of a is decremented by 1 .i.e. a becomes 9.There fore
a=9;
b=10;


06:27
Unknown
0 comments:
Post a Comment