Assignment operator

Learn about the assignment operator, the simplest operator in programming. See how it is used to put information into variables.

11/09/2023

youtube-thumb
algol-youtube-play

In fact, the assignment operator is the simplest type of computer programming operator. When it comes to variables, it is certainly the first category of operator that you should learn.

Basically, it allows you to modify the values of a variable.

If you still don’t know what a variable in programming is, you should certainly read this article in which we explain in detail about it.


What is the assignment operator?

Represented in most programming languages by the equality symbol =, the assignment operator has the purpose of placing a value inside a variable.

operador-atribuicao

In the example below, the assignment operator places the value “3500.00” inside the variable “salary”.

float salary = 3500.00;

That simple!


Active and passive role

Depending on the left/right position of a variable, the = operator assigns an active or passive role to it.

Just to illustrate, see the example below as the = operator assigns the variable firstValue the active and passive role at different times.

1.int firstValue = 50;
2.int secondValue = firstValue;

Note in line 1 that the firstValue variable is placed in the left position of the = assignment operator, in such a way that the variable has a passive role. That is, it will be modified by what is on the right side (the value 50).

1.int firstValue = 50;

It is as if the value 50 is thrown into the variable!

Now notice in line 2 that the variable firstValue is placed in the right position of the assignment operator =, in such a way that it now has an active role. That is, it modifies the secondValue variable.

2.int secondValue = firstValue;

It is as if a copy of the value contained in the variable “firstValue” (the value 50) was thrown into the variable “secondValue”!

In summary: what is on the right side of the operator = changes what is on the left side. That simple!


Do not confuse = with ==

There is a category of operators called relational operators. One of the existing operators in this category is that of equality, represented by the symbol ==.

It is very common for beginning students to confuse the = assignment operator with the == equality operator.

However, they have different purposes and work quite differently. So don’t confuse them!


The assignment operator is only the 1st of the 5 categories of programming operators . Continue your learning and read the next article about arithmetic operators.

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