Operators in Computer Science are just like operators in Maths: they are symbols which perform certain functions on data.
Arithmetic operators work on numbers. Most of them are basic Maths.
Table 1 shows the arithmetic operators.
Table 1
Name | Operator | Example | Result |
---|---|---|---|
Addition | + |
3 + 6 |
9 |
Subtraction | - |
7 - 2 |
5 |
Multiplication | * |
5 * 8 |
40 |
Division | / |
10 / 4 |
2.5 |
Integer (floor) division | // (or DIV ) |
11 / 3 |
3 |
Modulus (remainder) | % (or MOD ) |
12 % 5 |
2 |
Comparison operators give a boolean result.
Table 2 shows the comparison operators.
Table 2
Name | Operator | True | False |
---|---|---|---|
Equal to | == (or = ) |
7 == 7 |
7 == 8 |
Not equal to | != (or ≠ ) |
5 != 8 |
5 != 5 |
Less than | < |
4 < 6 |
4 < 3 |
Greater than | > |
6 > 5 |
6 > 6 |
Less than or equal to | <= (or ≤ ) |
3 <= 7 |
3 <= 2 |
Greater than or equal to | >= (or ≥ ) |
8 >= 8 |
8 >= 9 |
Boolean operators work on booleans.
Table 3 shows the boolean operators.
Table 3
Operator | True | False |
---|---|---|
AND |
True AND True |
True AND False False AND True False AND False |
OR |
True OR True True OR False False OR True |
False OR False |
NOT |
NOT False |
NOT True
|
What is the result of
(11 + 6) < (31 // 2) + (14 % 3)
?
False: this evaluates to 17 < 15 + 2
which is 17 < 17
.