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).
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.
,+ 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;
It takes exactly two operands – left and right. The result is their sum:
3 + 5 = 8; 6.4 + 3.14 = 9.24
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.
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;
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.
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;
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;
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 :).
They all work with exactly one left and one right operand. Both sides need to be of the same type.
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;
We can use these, when we modify the value of a variable. The next examples will explain this better:
Actions are taken right to left. So, first the
rightmost 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.
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.
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)
The same as above, but if the two sides are equal it returns “true”.
4 >= 3 (True) 0 >= 7 (False) 6 >= 6 (True)
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)
The same as the previous, but if the operands are equal it returns “true”.
5 <= 10 (True) 5 <= 7 (False) 5 <= 5 (True)
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)
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.
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.
Previous: What is a variable 
Next: Logical operators 
Tutorial Contents:
1)Learn
Computer Programming
2)Software Development Process
3)Flow
Chart
4)Flow
Chart Symbols
5)Data
Type
6)What is a variable
7)Math
Operators
8)Logical
Operators
9)Loops
10)Nested Loops
11)Arrays
12)Multidimensional arrays
13)Programming Questions
Did this help? Support me with your vote ;) 


Did this help? 

