# Math Operators

Let’s see which math operators we will use in programming. Probably you already know some of them. In the name of consistency we will separate them into three categories: arithmetical, assignment and comparing(relation).

#### Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

Before we review each of them, we will clarify one concept – operand. Each operator needs one or several parameters. These parameters are called operands. Consider the following:
3 + 5
In this example “+” is the sum operator, 3 is the left and 5 is the right operand.

## Sign operators

-,+ We put them before a number. The plus is not mandatory, because by default all numbers are positive. So we will use only the minus sign. Examples:
count = 5; (count = +5;)
temp = -10;

## Arithmetic math operators

#### Addition / Sum ( + )

It takes exactly two operands – left and right. The result is their sum:

3 + 5 = 8;    6.4 + 3.14 = 9.24

#### Increment ( ++ )

It has only one argument (a variable) – left or right. The result is the variable’s value, incremented by one.

number = 5;
number ++;
Now number is equal to 6.

iNumber = 5;
++iNumber;
Again iNumber is equal to 6.

When we use ++ with a left parameter(iNumber++), we call the operator “suffix” and when it has a right operand (++iNumber) – “prefix”. When used as a suffix, the increment is done, after the value of the variable is used in the statement. Used as a prefix, the increment is done before the calculations.

number = 5;
count = number++;
"number" has value 6, but "count" is equal to 5.

number = 5;
count = ++number;
Both variables are equal to 6.

#### Subtraction ( – )

Also has a left and a right parameter. It subtracts the right operand from the left.
5 – 2 = 3;     2 – 6 = -4;     7.5 – 3 = 4.5;

#### Decrement ( –– )

It is the same as ++, except that the variable is the decremented by 1. Can be used as prefix or suffix and the same rules apply.

#### Multiplication ( * )

Again – one left and one right. The result is the product of the two numbers.
3 * 2 = 6;     2 * (-6) = -12;     3 * 2.5 = 7.5;

#### Division ( / )

The result is the left part, divided by the right.
Attention:
if both operands are integers, the result will always be an integer!

4 / 2 = 2;     5/2 = 2!     5/2.0 = 2.5;

#### Division with a remainder ( % )

Accepts left and right argument. They must be integers. Returns only the remainder from their division.

4 % 2 = 0;   3 % 2 = 1;   23 % 4 = 3;

OK, we are done with the simple arithmetic operators :).

## Assignment math operators

They all work with exactly one left and one right operand. Both sides need to be of the same type.

#### Equal ( = )

This is the simplest, yet the most used one. First the right part is taken. If it is a value ready to use, the left part saves that value and that’s all. Examples:
iNumber = 5;     count = iNumber;

If the right argument needs calculation – then first that calculation is done. After that our program will assign the result to the left operand. Examples:
number = 4 + 6;     count = number * 2;

#### Short versions

We can use these, when we modify the value of a variable. The next examples will explain this better: #### Multiple assignments ( a = b = c )

Actions are taken right to left. So, first the right-most argument is calculated. After that, the result is assigned to the left.

number = count = 5;

Both variables will take the value 5. count takes the value 5, then number takes the value of count(5).

Although this is correct, it ruins the code readability. Avoid to use of multiple assignments.

## Relation math operators

We use them to compare values and variables. The result is “true” or “false”. This is useful when we want to do some decision in our algorithm. All these operators take exactly one left and one right operand.

#### Greater than ( > )

If the left is greater than the right, the result is “true”, otherwise it’s “false”.

4 > -3 (True)     0 > 7 (False)     6 > 6 (False)

#### Greater than or equal to ( >= )

The same as above, but if the two sides are equal it returns “true”.

4 >= -3 (True)     0 >= 7 (False)     6 >= 6 (True)

#### Less than ( < )

If the left part has smaller value than the right, the result is “true”, otherwise it’s “false”.

5 < 10 (True)     5 < -7 (False)     5 < 5 (False)

#### Less than or equal to ( <= )

The same as the previous, but if the operands are equal it returns “true”.

5 <= 10 (True)     5 <= -7 (False)     5 <= 5 (True)

#### Equals ( == )

Returns “true” only when both sides are equal.
*Note that the comparison symbol is the double “=”. In some languages, like Pascal, this is not the case (it uses = for comparison and := for an assignment). Anyway, we are working with the symbols accepted in the most popular languages.

5 == -4 (False)     5 == 7 (False)     5 == 5 (True)

#### Not equal ( != )

Returns “true” if the operands are different, otherwise – “false”.

5 != -4 (True)     5!= 7 (True)     5 != 5(False)

One more note when working with relation math operators: It is possible to have unexpected results when comparing real numbers. This is because of the way they are presented in the computer.

## Example task using the basic math operators

Design an algorithm, which takes two numbers and compares them. If the first is bigger, divide it by two and increment the second number. Otherwise multiply the first number by 2 and subtract 2 from the second number. If you feel unsure about anything, feel free to review this or any of the previous lessons again.

## Homework

1. Create a plan for a program which calculates the sum of three numbers.
2. Design an algorithm that finds if a given number is odd or even(even numbers can be divided by 2 with a remainder 0).
3. Swap the values of two variables. (You will need an extra variable).
4. Find if the second of three given numbers is the greatest.
5. Find the biggest of three numbers.
 Previous: What is a variable Next: Logical operators

#### Do you learn better from video?

Learn faster with deeper understanding!
The "Computer programming for beginners" course is the perfect place to begin with programming.

› Math operators

Tutorial Contents:

 Did this help? Support me with your vote ;-)
 Did this help?