Mathematical operations

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. Variables

    /

  5. Operations

    /

  6. Mathematical operations

The mathematical operators

In C/C++, the symbols used to represent mathematical operations are called the mathematical operators. They are all used the same way as the addition operator +.

Here is a table with the mathematical operators:

OperationSymbol (Operator)
Addition+
Subtraction-
Multiplication*
Division/
Modulo%

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() { float n=5, n2 = 20; n *= n2; n /= n2; n -= n; n += 43; n + 7; return 0; }

In the example above, at first, the variable n equals 5. Then, it is multiplied by n2, which equals 20, and is assigned with the value, so it holds the number 100. Then, n is divided by n2, which stills equal 20, and is assigned with the result, so it equals 5 again. After that, the variable n is subtracted by its own value and is assigned with the result, so it holds the number 0. Then, n is added with the number 43 and is assigned with the result, so it equals 43. Finally, the value of n is added with the number 7, but nothing is done with the result, so n stills hold the number 43.

Again, in the example above, if the operators were not followed by an equal sign =, the value of n would not have changed at all, because it is the assignment operator = that changes the value a variable holds.

We learned that all operations return a value. This is the case for the operators followed by the equal sign = too.

In the following example:

int var1 = 5, var2 = 6; var1 = var2 *= var1;

Both the variables var1 and var2 are assigned with the number 30. What happens is that the operator *= calculates the multiplication of the values of the variables var1 and var2, assign the result to var2 and returns the result. Then, the assignment operation with var1 as left operand catches the result of the *= operation as its right operand.

Modulo

For those who do not know, the result of a modulo operation is the remainder of a division between two integer operands.

For example, the result of a modulo operation between the numbers 5 and 5 (5%5) is 0. The modulo of 5 and 2 (5%2) is 1. We can fully divide 5 with 2 only 2 times, then the value that is lasting is 1. That is the modulo.

Note that modulo operations are only defined for signed and unsigned integers (Not with floating point numbers).

Parentheses

Parentheses can be used for setting the priorities on operations.

Example:

n1 = (n2 + 5) * 6;

Without the parentheses, 5 would have been multiplied by 6 and then the result (30) would have been added to the value of the variable n2. With the parentheses, the variable n2 will added with the number 5 and then the result will be multiplied by 6.

Iteration

To add the value 1 to a variable, it is possible (And almost always done) to simply use the ++ operator:

variable++;

Instead of:

variable+=1;

++ is basically a (small) shortcut to +=1 that exists because adding the value 1 to a variable actually happens often.

We can also use the -- operator to subtract the value 1 from a variable:

variable--;

Instead of:

variable-=1;

It is also possible to put the ++ and the -- operators before the name of the variable:

++variable; --variable;

There is however one difference. If the ++ or -- operator is placed before the name of the variable, the operation is executed and then the result is returned. If it is placed after, the value of the variable is returned and then the operation is executed.

In the following two lines, the variable var2 is assigned with the value 0:

int var = 0; int var2 = var++;

In the following two lines, the variable var2 is assigned with the value 1:

int var = 0; int var2 = ++var;

More complex operations

What has been shown above is only the “building bricks” of operations on variables.

We could perfectly write the following line of code:

n1 += 7 * (n1+n2) * (n1-n2) / n3 – n2;