Language: EN

programacion-operadores-aritmeticos

Arithmetic operators

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

In addition, there are also other arithmetic operators that you may be less accustomed to, such as calculating the remainder, 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 operations as in mathematics. Basically, we all know how these operators work.

However, it should be noted that in many languages you will get different results when performing operations with whole numbers or decimal numbers. This can lead to some surprise and misunderstanding, especially at the beginning. We will see this in more detail in the examples below.

On the other hand, exponentiation is not usually common in languages. When it is, it is usually represented by ^ or **. However, in most languages, exponentiation is usually a function in some library like

Math.Pow(a, b)

On the other hand, the modulus % is the remainder of the integer division of two whole numbers. When you are taught to divide without decimals, and you are told “9 divided by 7 is 1, and I carry 2”… well, that “2” is the modulus.

The increment ++ and decrement -- operators increase or decrease, respectively, the value of a variable by 1. They usually exist in prefix version ++i or postfix version i++. Although in practice, it is recommended that they be 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 suma = a + b;             // suma = 17
int resta = a - b;            // resta = 7
int multiplicacion = a * b;   // multiplicacion = 60
int division = a / b;         // division = 2
int modulo = a % b;           // modulo = 2

Very similar is the case of JavaScript JavaScript.

let a = 12;
let b = 5;
let suma = a + b;             // suma = 17
let resta = a - b;            // resta = 7
let multiplicacion = a * b;   // multiplicacion = 60
let division = a / b;         // division = 2.4
let modulo = a % b;           // modulo = 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 resulted in a decimal value, while in the previous example, only 2 was returned, which is the result of the operation with whole numbers.

Finally, in the case of Python, we would have:

a = 12
b = 5
suma = a + b                 # suma = 17
resta = a - b                # resta = 7
multiplicacion = a * b       # multiplicacion = 60
division = a / b             # division = 2.4 (decimal result)
division_entera = a // b     # division = 2 (integer result)
modulo = a % b               # modulo = 2

In this Python example, 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 mentioned it before, and we saw it in the code examples. In many languages (typically strongly typed languages) the operations will be different if we work with whole numbers 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 whole numbers. This can cause some confusion for people who are just starting out.

If we performed the example with decimal values, we would get 2.4.

float a = 12;
float b = 5;

float division = a / b;     // division = 2.4

In languages with dynamic typing, such as JavaScript or Python, this conversion is often done “under the hood” without us realizing it. Therefore, the division gives us 2.4 directly.

However, integer operations are useful on many occasions. Thus, for example, Python provides an alternative operator // for integer division

a = 12
b = 5

division_entera = a // b     # division_entera = 2

While in JavaScript we would have to “make do” for example like this

a = 12
b = 5

let division_entera = Math.floor(a / b);     // division_entera = 2