arithmetic operators

“Mathematics is not about numbers, equations, computations, or algorithms, it is about understanding” William Paul Thurston

In this section we’re assuming you already read the operators section.

Arithmetic operators are those that we use for basic mathematical operations like addition, subtraction, multiplication and others that you can see in the table below.

Operator Symbol
Addition Operator +
Subtraction Operator
Multiplication Operator *
Division Operator /
Modulus Operator %
Increment Operator ++
Decrement Operator

The addition + operator is used to add together two values (operands). These values can be a variable, constant values (like 1.22, 2, 3,4 etc.) or the result of expression or the returned value of a function call.

Example:

#include <iostream>
using namespace std;
int main()
{
int integer = 10;
int result = 2 * integer;
cout<< result;
return 0;
}
Output: 20;

Subtraction `-` Operator:

This operator is used to subtract two operands (values) from each other. These values can be either a constant value like (10, 4 ,2, 43.22 etc.), the result of calling a function or a variable.

Example:

#include <iostream>
using namespace std;
int main()
{
int integer = 10;
int result = 2 - integer;
cout<< result;
return 0;
}

Output:

-8

Multiplication `*` operator:

When we want to multiply two values to each other, we use multiplication * operator. The values that can be used as the operands of the multiplication-operator can be the value assigned to a variable, constant values or the result of a function call.

Example:

#include <iostream>
using namespace std;
int main()
{
int integer = 20;
int result = integer * integer;
cout<< result;
return 0;
}

Output: 400

Note: the asterisk * symbol is used in pointers as well and it has totally different meaning when it comes to pointers.

Division `/` operator:

This operator is used to divide the operand on the left side by the operand on the right side of the / operator. Just like multiplication operator, the values that are applicable to this operator are constant values, variables and the returned value from calling a function.

Example:

#include <iostream>
using namespace std;
int sum(int valueOne, int valueTwo){
return valueTwo + valueOne;
}
int main()
{
int vOne = 100;
int vTwo = 200;
int result = 600 / sum(vOne , vTwo);
cout<< result;
return 0;
}

Output: 2

In this example, we have a variable called result and the value of this variable is the result of 600 / sum(vOne , vTwo) expression. In this expression the value 600 is divided by the result of calling to sum(vOne, vTwo) function which took two arguments and retuned the sum of two values we set as its argument.

Modulus `%` Operator:

Let me ask a question: how many 2 is in 9?

Well if you think about it, it is 4. This is because if we multiply 5 to 2 we will get 10 which is one value over 9 but if we multiply 4 by 2 the result is 8. Now if we subtract the value 9 from 8 what’s the remainder? It is 1.

We use modulus operator to get that remainder.

Example:

#include <iostream>
using namespace std;
int main()
{
int result = 13 % 4;
cout<< result;
return 0;
}

Output:

1

Note: the value we can use for this operator is constant values (like 1,2,3,4, 43.23 etc.), variables and the retuned value from a function call.

Increment `++` Operator:

This operator is used to increase the value assigned to a variable by one.

This operator can be set on the left or right side of the target variable.

Example:

#include <iostream>
using namespace std;
int
main() {

int valueRight = 13;
int valueLeft = 13;
int left = ++valueLeft;
int right = valueRight++;

cout<<"left-variable: "<<left <<", right-variable: "<<right<<", valueRight: "<<valueRight;

return 0;
}

Output:

left-variable: 14 , right-variable: 13, valueRight: 14

Here’s the difference:

If the ++ operator appeared on the left side of a variable like int left = ++valueLeft; that means first increase the value assigned to the operand (in this case valueLeft) and then proceed to the rest of operations. So the assigned value to the left variable will be 14.

On the other hand, if the ++ operator appeared on the right side of a variable like int right = valueRight++;, the value assigned to the operand (in this case valueRight) will be increased by one, but this is only guaranteed for the next time we call the target variable. So the final value that is assigned to the variable right in this example is 13 because the value of the valueRight did not change right away and the right variable took the old value stored in this variable.

Decrement `–` Operator:

This operator is used to decrease the value assigned to a variable by one.

Notes:

  • This operator can be set on the left and right side of the target
    variable.

  • Rules that applied to `++` operator also apply to `–` operator
    as well.

Example:

#include <iostream>
using namespace std;
int main() {
int valueRight = 13;
int valueLeft = 13;
int left = --valueLeft;
int right = valueRight-- + valueRight;
cout<<"left-variable: "<<left <<", right-variable: "<<right<<", valueRight: "<<valueRight;
return 0;
}

Output:

left-variable: 12, right-variable: 25, valueRight: 12

Note: The basic arithmetic operations mentioned in this section apply to operands from left to right.

For example if we have int res = 10/2; the value 10 is divided by 2 not vice versa!