Arithmetic operators

Learn about the 2nd category of operators. See how arithmetic operators are used to perform mathematical calculations in programming.

11/09/2023

youtube-thumb
algol-youtube-play

Before learning about arithmetic operators, it is first recommended that you already know the assignment operator.

If you still don’t know what the assignment operator is, you should certainly read this article in which we explain in detail about it.

But let’s get down to business!


What are arithmetic operators?

Arithmetic operators represent the 2nd category of programming operators. We use them to perform mathematical operations.

The main programming languages use arithmetic operators in order to perform the basic operations of mathematics:

+

which represents sum.

which represents subtraction.

*

which represents multiplication.

/

which represents division.

%

which represents the rest of the division.

However, we can observe that the symbols of the multiplication and division operators used in programming are different from those used in mathematics.

In computer programming:

operadores-aritmeticos

In mathematics:

operadores-aritmeticos

It is likely that you have also noticed that the operator of the rest of the division, represented by the symbol %, has no representation in mathematics!

In addition, some programming languages (such as Javascript and Python) also have the exponentiation operator, represented by the symbol **. It is used for the purpose of performing operations with powers.


How to use arithmetic operators?

Just to give an example, below is a small algorithm that demonstrates how to use arithmetic operators to perform operations with variables.

1.int varA = 5;
2.int varB = varA + 3;
3.int varC = varA * varB;

Line 1 shows the declaration of the variable “varA” and the placement of the value 5 inside it.

1.int varA = 5;

Line 2 is the first use of the addition arithmetic operator. The content of the variable “varA” (the value 5) is added to the value 3. This results in the value 8, which is, finally, placed inside the variable “varB”. The animation below illustrates how this happens:

2.int varB = varA + 3;

Then, in line 3, the multiplication operator is used. The value of the variable “varA” (the value 5) is multiplied by the value of the variable “varB” (the value 8). This results in the value 40, which is finally placed inside the variable “varC”. The animation below illustrates how this happens:

3.int varC = varA * varB;

Certainly, these examples were quite enlightening for you. Eventually, some specific doubts may arise about the use of these operators in certain situations.

So, keep learning! Read our article on conditional statements and continue your studies.

autor

David Santiago

Master in Systems and Computing. Graduated in Information Systems. Professor of Programming Language, Algorithms, Data Structures and Development of Digital Games.

Outros artigos