Language: EN

programacion-operadores-aritmeticos

Arithmetic operators

Arithmetic operators are used to perform basic mathematical operations (such as addition, subtraction, multiplication, and division)

Moreover, there are also other arithmetic operators that you may be less familiar with, such as calculating the modulus or incrementing or decrementing a variable by one.

The most common arithmetic operators are:

OperatorExampleResult
Addition (+)5 + 38
Subtraction (-)10 - 46
Multiplication (*)2 * 612
Division (/)15 / 35
Exponentiation (^)3 ^ 29
Modulus (%)7 % 21
Increment (++)a = 5; a++6
Decrement (—)b = 8; b—7

The addition, subtraction, multiplication, and division operators are the same as in mathematics (basically, we all know how these operators work).

However, it should be noted that in many languages, different results will be obtained when performing operations with integers or decimal numbers. This can lead to some surprises and misunderstandings, especially at the beginning (we will see this further down in the examples).

On the other hand, exponentiation is not commonly found in languages. When it is present, it is usually represented by ^ or **. However, in most languages, exponentiation is typically a function from a library like

Math.Pow(a, b)

The modulus % is the remainder of the integer division of two integers. (Do you remember when you were taught to divide without decimals, and they said “9 divided by 7 is 1, and I have 2 left”?.. well, that “2 left” 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 that they be on their own line.

Use of 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