Operator
|
Performs
|
+
|
Additon
|
-
|
Subtraction
|
/
|
Division
|
*
|
Multiplication
|
%
|
Gives Remainder
|
++
|
|
--
|
int operand1 = 30; int operand2 = 10; println("Addition of operand1 and operand2 is " + (operand1 + operand2)); println("Subtraction of operand1 and operand2 is " + (operand1 - operand2)); println("Multiplication of operand1 and operand2 is " + (operand1 * operand2)); println("Division of operand1 and operand2 is " + (operand1 / operand2)); println("Remainder of operand1 and operand2 is " + (operand1 % operand2));
When you ran above application, you can able to see below
messages in console.
Addition of operand1 and operand2 is 40 Subtraction of operand1 and operand2 is 20 Multiplication of operand1 and operand2 is 300 Division of operand1 and operand2 is 3 Remainder of operand1 and operand2 is 0
Some points to be
Noted
1. Multiplication,
Division and remainder operators has more priority than Addition and
Subtraction operators.
Operators.pde
int var1 = 10, var2 = 20; int result = var1+var2*var1; println(result); result = var1-var2/var1; println(result); result = var1+var2%var1; println(result);
Output
210
8
10
Explanation
for the
Expression
result =
var1+var2*var1
=
10 + 20*10
=
10 +200
=
210
result =
var1-var2/var1
=
10-20/10
=
10-2
= 8
result =
var1+var2%var1
=
10 + 20 % 10
=
10 + 0
=
10
2. Remainder operator
always returns the sign of numerator
Ex:
5 % 2 = 1
-5 % 2 = -1
int var1 = 5, var2 = 2; println(var1%var2); var1= -5; println(var1%var2);
Output
1
-1
No comments:
Post a Comment