Arithmetic operators are used to perform basic mathematical operations (such as addition, subtraction, multiplication, and division).
Furthermore, there are other arithmetic operators you might be less familiar with, such as calculating the modulus, or incrementing or decrementing a variable by one unit.
The most common arithmetic operators are:
| Operator | Example | Result |
|---|---|---|
| Addition (+) | 5 + 3 | 8 |
| Subtraction (-) | 10 - 4 | 6 |
| Multiplication (*) | 2 * 6 | 12 |
| Division (/) | 15 / 3 | 5 |
| Exponentiation (^) | 3 ^ 2 | 9 |
| Modulus (%) | 7 % 2 | 1 |
| Increment (++) | a = 5; a++ | 6 |
| Decrement (—) | b = 8; b— | 7 |
The addition, subtraction, multiplication, and division operators are the same operations as in mathematics (basically, we all know how these operators work).
However, it should be noted that in many languages, results will differ when performing operations with integers or numbers with decimals. This can lead to some surprises and misunderstandings, especially at the beginning (we’ll see this in the examples below).
On the other hand, exponentiation is not common in most languages. When it is, it is usually represented by ^ or **. However, in most languages exponentiation is usually a function from some library, like
Math.Pow(a, b)
The modulus % is the remainder of the integer division of two integers. (Remember when they taught you to divide without decimals, and they said “9 divided by 7 is 1, with a remainder of 2”? Well, that “2 that remains” is the modulus).
Finally, the increment ++ and decrement -- operators increase or decrease, respectively, the value of a variable by 1. They usually exist in prefix ++i or suffix i++ versions. Although nowadays, the recommended practice is to have them on their own line.
Using Arithmetic Operators in Programming Languages
Next, we will see how to use arithmetic operators in different programming languages:
For example, this is how they would be applied in C, C++, C# or Java.
int a = 12;
int b = 5;
int sum = a + b; // sum = 17
int difference = a - b; // difference = 7
int multiplication = a * b; // multiplication = 60
int division = a / b; // division = 2
int modulus = a % b; // modulus = 2
The case is very similar in JavaScript JavaScript.
let a = 12;
let b = 5;
let sum = a + b; // sum = 17
let difference = a - b; // difference = 7
let multiplication = a * b; // multiplication = 60
let division = a / b; // division = 2.4
let modulus = a % b; // modulus = 2
Here we use the same variables a and b and the same arithmetic operators as in the previous example. However, in JavaScript, we use the keyword let to declare the variables.
Also, note that the division has yielded a result with decimals, while in the previous example, it only returned 2, which is the result of the operation with integer numbers.
Finally, in the case of Python, we would have:
a = 12
b = 5
sum = a + b # sum = 17
difference = a - b # difference = 7
multiplication = a * b # multiplication = 60
division = a / b # division = 2.4 (decimal result)
integer_division = a // b # division = 2 (integer result)
modulus = a % b # modulus = 2
In this example in Python, we use the same variables a and b and the same arithmetic operators. But, unlike the previous examples, in Python, it is not necessary to specify the data type or declare the variable.
Integer Arithmetic
We have mentioned this before, and we have seen it in the code examples. In many languages (typically strongly typed languages) operations will be different if we work with integers or decimals.
Let’s look at the case of division in C, C++, C# or Java
int a = 12;
int b = 5;
int division = a / b; // division = 2
In this case, the division is 2 because we are operating with integers. This can cause some confusion for people who are just starting.
If we were to perform the example with decimal values, we would obtain 2.4.
float a = 12;
float b = 5;
float division = a / b; // division = 2.4
In dynamically typed languages, such as JavaScript or Python, this conversion often happens “under the hood” without us noticing. Therefore, the division gives us 2.4 directly.
However, operations with integers are useful on many occasions. For example, Python provides an alternative operator // for integer division
a = 12
b = 5
integer_division = a // b # integer_division = 2
While in JavaScript we would have to “make do” like this
a = 12
b = 5
let integer_division = Math.floor(a / b); // integer_division = 2
