- Addition (
+
): Adds two operands.- Example:
sum = 3 + 2; // sum is 5
- Example:
- Subtraction (
-
): Subtracts the right operand from the left operand.- Example:
difference = 5 - 3; // difference is 2
- Example:
- Multiplication (
*
): Multiplies two operands.- Example:
product = 4 * 2; // product is 8
- Example:
- Division (
/
): Divides the left operand by the right operand.- Example:
quotient = 10 / 2; // quotient is 5
- Example:
- Modulus (
%
): Returns the remainder of division of the left operand by the right operand.- Example:
remainder = 10 % 3; // remainder is 1
- Example:
- Logical AND (
&
): Performs a logical AND operation on each bit; true if both operands are true.- Example:
result = true & 1; // result is true (true AND true)
- Example:
- Logical OR (
|
): Performs a logical OR operation on each bit; true if at least one operand is true.- Example:
result = false | 0; // result is false (true OR false)
- Example:
- Logical XOR (
^
): Performs a logical XOR operation on each bit; true if operands are different.- Example:
result = 1 ^ 0; // result is true (true XOR false)
- Example:
- Assignment (
=
): Assigns the value of the right operand to the left operand.- Example:
a = 5.3; // a is assigned the value 5.3
- Example:
- Not Equal (
!=
): Returns true if the operands are not equal.- Example:
isNotEqual = (5 != 3); // isNotEqual is true
- Example:
- Less Than (
<
): Returns true if the left operand is less than the right operand.- Example:
isLess = (3 < 5); // isLess is true
- Example:
- Greater Than (
>
): Returns true if the left operand is greater than the right operand.- Example:
isGreater = (7 > 4); // isGreater is true
- Example:
- Equal (
==
): Returns true if the operands are equal.- Example:
isEqual = (5 == 5); // isEqual is true
- Example:
- Less Than or Equal To (
<=
): Returns true if the left operand is less than or equal to the right operand.- Example:
isLessOrEqual = (3 <= 5); // isLessOrEqual is true
- Example:
- Greater Than or Equal To (
>=
): Returns true if the left operand is greater than or equal to the right operand.- Example:
isGreaterOrEqual = (7 >= 4); // isGreaterOrEqual is true
- Example:
Compound Assignments
Arithmetic operators (+
, -
, *
, /
, %
) can be combined with the assignment operator (=
) to update a variable’s value directly after performing the operation. This is known as compound assignment:
- Addition Assignment (
+=
):a += b
is equivalent toa = a + b
. - Subtraction Assignment (
-=
):a -= b
is equivalent toa = a - b
. - Multiplication Assignment (
*=
):a *= b
is equivalent toa = a * b
. - Division Assignment (
/=
):a /= b
is equivalent toa = a / b
. - Modulus Assignment (
%=
):a %= b
is equivalent toa = a % b
.
These operators provide a shorthand way to update variables, making code more concise and potentially improving readability.