Operators


Martin McBride, 2017-01-31
Tags none
Categories none

In programming, an operator is a symbol which specifies an action. For example:

x = y + 3

Here, + is an operator that tells the program to add two values. = is an operator that tells the program to assign the value on the right hand side to the variable on the left hand side.

To complete the jargon, in this example x, y, and 3 are called the operands.

Basic arithmetic operators

There are four basic operators. You will probably be familiar with them if you have used a spreadsheet or done any kind of programming before.

Addition - the addition operator, +, is used to add two values.

//Sets x = 5
x = 2 + 3

Subtraction - the subtraction operator, -, is used to subtract one value from another.

//Sets x to -1
x = 2 - 3

Negation - You can also use the - operator on its own, called a unary minus. It is called a unary operator because it only has one operand. The minus operator simply gives the negative of its operand.

//sets x to -4
y = 4
x = -y

Multiplication - the multiplication operator, *, is used to multiply two values.

//Sets x to 6
x = 2 * 3

Division - the division operator, usually /, is used to divide one number by another.

In computing there are 2 types of division. Floating point division gives and exact result. For example, in this case x will be assigned the value 2.5:

//Sets x to 2.5
x = 5.0 / 2.0;

Integer division (DIV) gives and integer value. This is sometimes called the quotient. For example, in this case x will be assigned the value 2. That is because if you divide 5 by 2 and ignore the fraction part you get 2:

//Sets x to 2
x = 5 DIV 2;

{{% orange-note %}} Pascal uses DIV for the quotient (integer division) operator. Python uses a double slash, //. Many languages use a single slash / - they automatically calculate the quotient if both operands are integers, but use normal division if one or both operands is a float. {{% /orange-note %}}

The way integer division works also varies slightly between languages.

Other operators

Modulo - the modulo operator calculates the remainder when an integer division is applied. It uses the % symbol (or mod for Pascal). In this example, 11 divided by 3 is 3 remainder 2, so the modulo value is 2.

//Sets x to 2
x = 11 % 3;

Modulo has some important uses:

  • x % 2 is zero if x is even, 1 if x is odd.
  • x % 10 gives the lowest digit of x, for example 437 % 10 gives 7.
  • x % n is zero if (and only if) x divides by n.

Exponentiation - the exponentiation power operator (also called the power operator) raises a number to a power. For example 2**3 is 2 to the power 3 ( 2 times 2 times 2, or 2 cubed), which is 8.

//Sets x to 8
x = 2 ** 3;

{{% green-note %}} Python and some versions of Pascal use , some languages use ^**. Some languages don't have a power operator, they provide a function instead. {{% /green-note %}}

Brackets

When a computer performs a calculation, it performs multiply and divide first, and then add and subtract. Just like normal maths. So in this case it will multiply 2 by 5, and then add 4, giving 14:

//Sets x to  14
x = 4 + 2 * 5;

You can use brackets () to change the order of calculation, just like you would in maths. In this case we add 4 and 2 first, then multiply by 5:

//Sets x to  30
x = (4 + 2) * 5;

Assignment operators

Quite often in code we want to change the value of a variable. For example, we might want to increase the value of x by 3. We can do this with the following code:

x = x + 3;

As a shortcut, most languages allow you to do this:

x += 3;

This is purely a shortcut, both lines of code are valid, and both do exactly the same thing. But the second method is usually preferred because:

  • It is shorter, and means less typing.
  • More importantly, it makes it clear what the code is intended to do. The first form just sets x equal to an expression which happens to also include x. The second form instantly tells you what the code intends to do - add something to the current value of x.

Most languages support this, although standard Pascal doesn't.

{{% blue-note %}} There are equivalent assignment operators for most other operations: -=, =, /=, %=, *=, and the bitmap operators. {{% /blue-note %}}

Copyright (c) Axlesoft Ltd 2021