In this article,Assignment and Unary Operators are explained in detail with examples.We are going to learn about Assignment operators with an example.
This operator is used to assign to value to a variable. It is "=".
Ex
a=10;
b=20;
sum=a+b;
Compound Assignment. (From topic Assignment and Unary Operators)
If assignment operator is used with basic arithmetic operators then we get compound assignments.
Operator
|
Compound assignment
|
+=
|
a+=b =>a=a+b
|
-=
|
a-=b =>a=a-b
|
*=
|
a*=b =>a=a*b
|
/=
|
a/=b =>a=a/b
|
%=
|
a%=b =>a=a%b
|
Ex
//Program related to Assignment and Unary Operators-compound assignment
class CompoundAssignment
{
public static void main(String args[])
{
int a,b;
int sum;
a=10;
b=20;
sum=a+b;
System.out.println("Sum is: "+sum);
System.out.println("A+=B is: "+(a+=b));
System.out.println("A-=B is: "+(a-=b));
System.out.println("A*=B is: "+(a*=b));
System.out.println("A/=B is: "+(a/=b));
System.out.println("A%=B is: "+(a%=b));
}
}
Output
Sum is: 30
A+=B is: 30
A-=B is: 10
A*=B is: 200
A/=B is: 10
A%=B is: 10
These operators take one value or operand to perform the operation.They are
++ (Increment) => increase the value by 1.
-- (Decrement) => decreases the value by 1.
- (Unary minus)=> negation of given value.
++ and -- is used in two ways.
Pre-operation Ex: ++a, --b
Post-operation Ex: a++, b++
Note
Whether pre-operation/post-operation, the value is just increased by one. Similarly, pre-decrement/ post-decrement value is just decreased by one.
However, the utility changes if assign to an another variable. (From topic Assignment and Unary Operators)
case 1: a=10,b=20;
b=++a;
first increment 'a' and assign a value to b (b=11).
case2: a=10,b=20;
b=a++;
assign 'a' to 'b' and then increment 'a' (b=10).
Unary minus: It is simply a negative of a number.
Ex: a=10;
a=-10;
Ex
// program related to Assignment and Unary Operators- Increment,Decrement.
class IncrementDecrement
{
public static void main(String args[])
{
int x=10,y=20;
System.out.println("x is: "+x);
System.out.println("y is: "+y);
System.out.println("++x is: "+(++x));
System.out.println("y++ is: "+(y++));
System.out.println("x is: "+x);
System.out.println("y is: "+y);
System.out.println("--x is: "+(--x));
System.out.println("y-- is: "+(y--));
System.out.println("x is: "+x);
System.out.println("y is: "+y);
x=10;y=20;
y=x++;
System.out.println("x is; "+x);
System.out.println("y is; "+y);
x=10;y=20;
y=++x;
System.out.println("x is: "+x);
System.out.println("y is: "+y);
x=10;y=20;
x=-x;
y=-y;
System.out.println("x is: "+x);
System.out.println("y is: "+y);
}
}
Output
x is: 10
y is: 20
++x is: 11
y++ is: 20
x is: 11
y is: 21
--x is: 10
y-- is: 21
x is: 10
y is: 20
x is; 11
y is; 10
x is: 11
y is: 11
x is: -10
y is: -20
Continue to next topic Ternary and Bitwise operators.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you...
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment