Using operators
In computer science, an operatorA character, or characters, that determine what action is to be performed or considered. 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 languageAlso known as high-level language. This is a computer programming language used to write programs. High-level languages need to be translated into machine code through a compiler, interpreter or assembler..
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 |
| Arithmetic operation | Addition |
|---|---|
| Operator | + |
| Example | x = x + 5 |
| Arithmetic operation | Subtraction |
|---|---|
| Operator | - |
| Example | x = x - 5 |
| Arithmetic operation | Multiplication |
|---|---|
| Operator | * |
| Example | x = x * 5 |
| Arithmetic operation | Division |
|---|---|
| Operator | / |
| Example | x = x / 5 |
| Arithmetic operation | Integer division |
|---|---|
| Operator | DIV |
| Example | x = x DIV 5 |
| Arithmetic operation | Remainder |
|---|---|
| Operator | MOD |
| Example | 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 |
| Relational operation | Assignment |
|---|---|
| Operator | = |
| Example | x = 5 |
| Relational operation | Equivalence |
|---|---|
| Operator | = or == |
| Example | if x = 5 or if x == 5 |
| Relational operation | Less than |
|---|---|
| Operator | < |
| Example | if x < 5 |
| Relational operation | Less than or equal to |
|---|---|
| Operator | <= |
| Example | if x <= 5 |
| Relational operation | Greater than |
|---|---|
| Operator | > |
| Example | if x > 5 |
| Relational operation | Greater than or equal to |
|---|---|
| Operator | >= |
| Example | if x >= 5 |
| Relational operation | Does not equal |
|---|---|
| Operator | <> or != |
| Example | 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 |
| Logical operation | And |
|---|---|
| Operator | AND |
| Example | if x > 0 AND x < 10 |
| Logical operation | Or |
|---|---|
| Operator | OR |
| Example | if topic == "Computing" OR topic == "Computer Science" |
| Logical operation | Not |
|---|---|
| Operator | NOT |
| Example | while NOT x |