Assignment Operators
Assignment operators are used to assign a value to a variable. The most common assignment operator is the equals sign (=).
For example, let's say we have a variable called x
and we want to assign the value of 5 to it. We can do it like this:
This means that the value of x
is now 5. We can also use other arithmetic operators in combination with the equals sign to perform a calculation and then assign the result to the variable. For example:
This means that the value of y
is now 15. We can also use other arithmetic operators in combination with the equals sign:
This means that the value of z
is now 15.
There are also other assignment operators like *=
, /=
, and %=
that work in the same way. They perform an arithmetic operation and then assign the result to the variable.
This means that the value of a
is now 10.
=
- Assignment Operator+=
- Addition Assignment-=
- Subtraction Assignment*=
- Multiplication Assignment/=
- Division Assignment%=
- Modulus Assignment**=
- Exponentiation Assignment
In the exercise below, you will have a variable x
with value 3
. Select the correct assignment operator to assign the value of each result.
x ?? x // output: 0
x ?? x // output: 1
x ?? x // output: 6
x ?? x // output: 9