In computer science, an operator is a character or characters that determine the action that is to be performed or considered.
There are three types of operator that programmers use:
- arithmetic operators
- relational operators
- logical operators
These operators are common to most high-level programming languages.
Arithmetic operators
Computers are designed to carry out calculations. Arithmetic operators allow arithmetic to be performed on values.
Arithmetic operation | Operator | Example |
---|---|---|
Addition | + | x = x + 5 |
Subtraction | - | x = x - 5 |
Multiplication | * | x = x * 5 |
Division | / | x = x / 5 |
Integer division | DIV | x = x DIV 5 |
Remainder | MOD | x = x MOD 5 |
Relational operators
Relational operators allow for assignment and enable comparisons to be made. They are used in condition testing.
Relational operation | Operator | Example |
---|---|---|
Assignment | = | x = 5 |
Equivalence | = or == | if x = 5 or if x == 5 |
Less than | < | if x < 5 |
Less than or equal to | <= | if x <= 5 |
Greater than | > | if x > 5 |
Greater than or equal to | >= | if x >= 5 |
Does not equal | <> or != | If x <> 5 or if x != 5 |
Logical operators
Logical operators are used to combine relational operators to give more complex decisions.
Logical operation | Operator | Example |
---|---|---|
And | AND | if x > 0 AND x < 10 |
Or | OR | if topic == "Computing" OR topic == "Computer Science" |
Not | NOT | while NOT x |