Page 63 - iPro_trackGPT_V5_Class8
P. 63
Example
Operator Description Syntax Output
(int a = 11, b = 0)
++ Returns the incremented value as result a ++ b = a++ a=12 b=11
increment
-- Returns the decremented value as result a -- b = a-- a=10 b=11
decrement
Assignment Operators
Assignment operators are used to assign values to variables. The equal sign (=) is the most
common assignment operator. You can combine it with arithmetic operators to create shorthand
assignment operators like +=, -=, *=, /=, and %=. These operators perform the arithmetic
operation on the variable's current value and then store the result back in the same variable on
the left side."
Example
Operator Description Syntax Output
(int a = 10, b = 2)
Returns the result to the operand present
a += b a = 12
+= on the left hand side after performing a += b
a += 5 a = 15
the addition operation
Returns the result to the operand present
a -= b a = 8
-= on the left hand side after performing a -= b
a -= 5 a = 5
the subtraction operation
Returns the result to the operand present
a *= b a = 20
*= on the left hand side after performing a *= b
a *= 5 a = 50
the multiplication operation
Returns the result to the operand present
a /= b a = 5
/= on the left hand side after performing a /= b
a /= 5 a = 2
the division operation
Returns the remainder to the operator
a %= b a = 0
%= present on the left hand side after a %= b
a %= 6 a = 4
performing the division operation
WRITING SOME MORE PROGRAMS
Let us write programs using Java's various concepts.
Program 1: Write a program to calculate the area and perimeter of a rectangle.
public class Calculate {
public static void main(String args[]) {
int length, width, area, perimeter;
length = 5;
width = 10;
// Area of rectangle = length * width
Output
Program Coding 61

