Operations on variables

  1. Courses


  2. Complete C++ Course


  3. The basics


  4. Variables


  5. Operations


  6. Operations on variables

Working with variables

Earlier, we learned what variables and primitives are. Now, it is time to learn to work with them.

Defining a variable

We saw, earlier, how to define (create) a variable, but here is a reminder: we write the type of the variable followed by its name and a semicolon ;.


int number;


In C/C++, symbols used to execute operations on variables are called operators. For example, symbol = is called the assignment operator or simply the = operator. Operators usually operate on two variables. Those variables are called the operands of the operation. The variable on the left of the symbol is called the left operand and the one on the right is called the right operand.

Setting the value of a variable

We assign a value to a variable using the operator =. The variable which we want to change the value must be the left operand and the value we want to assign must be the right one. The variable must have been previously defined (It must exist).


number = 5; variable = otherVariable;

(Do not forget the semicolon at the end of each instruction!)

Setting the value of a variable at its definition

When we define a variable without specifying its value, the content of the variable is undefined (It contains what was on the memory space before it was reserved for the variable). It could be anything. We can, if we want, set the value of a variable at the same time we define it.


int number = 5;

It is also possible, in C++ only, not in C, to do it that way:

int number(5);

Defining many variables of the same type at once

We can define many variables of the same type at the same time, by writing the type once and separating the name of the variables by commas.

Like that:

int var1, var2, var3;

We can also specify (set) their value:

float var1 = 4.5, var2, var3(9.4);

Mathematical operations on variables

Let us say we defined the 3 following variables: n1, n2 and n3. If we wanted n1 to hold to result of the summation of n2 and n3, we would have to write the following:

n1 = n2 + n3;

Note that in the line above, the values of n2 and n3 did not change. That means that the following line of code has no effect:

n2 + n3;

The reason is that, in the line of code above, the computer calculates the summation of n2 and n3, but since it is not told what to do with it, it just throws it away.

Note that in order for a variable to have its value changed by an operation, the operator used must contain the equal sign =. It also always is the left operand that is changed.

The addition operator + simply adds up the operands and return the result. It does not alter the value of the operands.

Operations return values

All operations return a value. Even the assignment operation = returns one! It returns the value it just assigned.

By writing the following:

var1 = var2 = 5;

Both var1 and var2 are assigned with the value 5. What happens in the line above is that the assignment operator assign var2 with the value 5 and returns the number 5. The number 5 returned is then received as right operand for the assignment operation on var1.

Adding something to a variable

Let us say we want to add the values of the variables n2 and n1 together and assign the result to the variable n1. We could write:

n1 = n1 + n2;

But we could also simply write:

n1 += n2;

The line above is almost always used over the one above it since it is more concise. The operator += basically means: Add the value of the left operand with the value of the right operand and assign the left operand with the result.

Note that the symbol = can be added after any mathematical operator to cause the left operand to receive the result of the operation.