Assignment operator

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

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-de-atribuição-assignment-operator-01

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!

Exercise

Since you have already understood the concept of assignment operator, correctly answer the exercise below:

The assignment operator is only the 1st of the 5 categories of programming operators. Continue your learning and see our articles about operators:

Either way, we recommend that you follow the standard learning flow and read the next article on arithmetic operators.

David V. Santiago

David V. Santiago

MSc in Systems and Computing, Professor of Computer Programming, Algorithms and Data Structures.

Was this article helpful to you?

So support us and share it with others who are interested in this subject!

Share on whatsapp
WhatsApp
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on email
Email

Follow us
on our social media.

This website uses cookies to ensure you get the best experience on our website.