Assignment Operators
Common assignment operators are:
- = : Assigns the value on the right to the variable on the left.
- += : Adds the right operand to the left operand and assigns the result to the left operand.
- -= : Subtracts the right operand from the left operand and assigns the result to the left operand.
- *= : Multiplies the left operand by the right operand and assigns the result to the left operand.
- /= : Divides the left operand by the right operand and assigns the result to the left operand.
- %= : Takes the modulus of the left operand by the right operand and assigns the result to the left operand.
Syntax Example:
Program
- int num = 10; // = operator
- num += 5; // num = num + 5
- num -= 2; // num = num - 2
- num *= 3; // num = num * 3
- num /= 4; // num = num / 4
- num %= 2; // num = num % 2
INPUT
public class AssignmentOperatorsExample { public static void main(String[] args) { // Assign a value int num = 10; System.out.println("Initial Value: " + num); // Using += operator num += 5; System.out.println("After += 5: " + num); // Using -= operator num -= 2; System.out.println("After -= 2: " + num); // Using *= operator num *= 3; System.out.println("After *= 3: " + num); // Using /= operator num /= 4; System.out.println("After /= 4: " + num); // Using %= operator num %= 2; System.out.println("After %= 2: " + num); } }
OUTPUT
![]()