2.3. Operators
Operators in programming languages are symbols that tells the compiler or interpreter to perform specific operations. The operand combined with the operator makes an operation. In an operation, an operand is the data that is being operated on. The position of the operator, with respect to its operands, may be prefix, infix or postfix.
Common simple operators include
arithmetic (e.g. addition with +),
comparison (relational) (e.g. “greater than” with >),
logical (e.g. AND, also written && in some languages), and
assignment (e.g., =, +=) operators.
Additional types of operators include assignment (usually = or :=), field access in a record or object (usually .), and bitwise and shift operators.
Operators can be categorized based on the number of operands required. The three categories of operators based on the number of operands they require are:
Unary operators: which require one operand (e.g., ++, !)
Binary operators: which require two operands (e.g., +)
Ternary operators: which require three operands (e.g., condition ? consequent : alternative)
2.3.1. Arithmetic Operators
Operator |
Name |
Description |
Example |
---|---|---|---|
|
Addition |
Adds together two values |
x + y |
|
Subtraction |
Subtracts one value from another |
x - y |
|
Multiplication |
Multiplies two values |
x * y |
|
Division |
Divides one value by another |
x / y |
|
Modulus |
Returns the division remainder |
x % y |
|
Increment |
Increases the value of a variable by 1 |
x++ |
|
Decrement |
Decreases the value of a variable by 1 |
x– |
Examples of arithmetic operators:
> x = 1;
> x++ // post-increment operator; x++ increments the value of variable x after it's evaluated in an expression
1
> x
2
> x = 2;
> ++x // pre-increment operator; ++x increments the value of variable x before it's evaluated in an expression
3
2.3.2. Comparison Operators
Comparison operators, also known as relational operators, compare
their operands (numbers or strings) and return a value of either
True
(1) or False
(0). These two values are known as Boolean values
and are used in conditional statements for decision making.
Operator |
Name |
Example |
---|---|---|
|
Equal to |
x == y |
|
Not equal to |
x != y |
|
Greater than |
x > y |
|
Less than |
x < y |
|
Greater than or equal to |
x >= y |
|
Less than or equal to |
x <= y |
Examples of comparison operations:
> int x = 1, y = 2;
> x == y
false
> x < y
true
> x <= y
true
>
2.3.3. Logical Operators
Logical Boolean operators [#_logical-operators]]_ are used to determine the logic between variables or values. Just like comparison operators, logical operators return True or False as a result of the evaluation.
Operator |
Name |
Description |
Example |
---|---|---|---|
|
Logical negation operator |
Returns logical negation of its operand |
!true // False |
|
Logical and |
Returns True if both statements are true |
x < 5 && x < 10 |
|
Logical or |
Returns True if one of the statements is true |
x < 5 || x < 4 |
Examples of logical operators are:
> int x = 2;
> x < 5 && x < 10
true
> x < 5 || x < 10
true
> !(x < 5 && x < 10)
false
2.3.4. Assignment Operators
The =
operator is called the simple assignment operator, which serves to assign a value to a variable (note that ==
is the equality test operator testing if two operands are equal).
In addition to simple assignment operator, compound assignment operators are shortcuts that do a math operation and assignment in one step. [2] :
No. |
Operator |
Example |
Same As |
Output of x == 5 |
Description |
---|---|---|---|---|---|
1 |
= |
x = 5 |
x = 5 |
5 |
Simple assignment operator; assigns values from right operands to left operand |
2 |
+= |
x += 3 |
x = x + 3 |
8 |
Add AND assignment operator; adds right operand to the left operand and assign the result to left operand |
3 |
-= |
x -= 3 |
x = x - 3 |
2 |
Subtract AND assignment operator; subtracts right operand from the left operand and assign the result to left operand |
4 |
*= |
x *= 3 |
x = x * 3 |
15 |
Multiply AND assignment operator; multiplies right operand with the left operand and assign the result to left operand |
5 |
/= |
x /= 3 |
x = x / 3 |
1.6666666666666667 |
Divide AND assignment operator; divides left operand with the right operand and assign the result to left operand |
6 |
%= |
x %= 3 |
x = x % 3 |
2 |
Modulus AND assignment operator; takes modulus using two operands and assign the result to left operand |
7 |
&= |
x &= 3 |
x = x & 3 |
1 |
Bitwise AND assignment operator ([3]) |
8 |
|= |
x |= 3 |
x = x | 3 |
Bitwise inclusive OR and assignment operator |
|
9 |
^= |
x ^= 3 |
x = x ^ 3 |
Bitwise exclusive OR and assignment operator |
|
10 |
>>= |
x >>= 3 |
x = x >> 3 |
Right shift AND assignment operator |
|
11 |
<<= |
x <<= 3 |
x = x << 3 |
Left shift AND assignment operator |
|
12 |
=> |
Lambda operator |
Footnotes