teach-ict.com logo

THE education site for computer science and ICT

2. Assignment Operators

Assignment operators assign values.

For the SQA exam board, the most common assignment operator is "=".

An operator always acts on at least one item, which is called the 'argument' of the operator.

In the case of assignment, '=' copies the value of its right argument into the argument on its left.

MyVariable = 2

This assigns the value 2 to the variable named 'MyVariable'.

The assignment operator can also copy a value from one variable to another. For example

MyVariable = AnotherVariable

Because of the right-to-left rule, that statement is going to copy the value of the right variable AnotherVariable into the variable on the left MyVariable - not the other way around.

Because of the assigment operator, you can write code to carry out calculations and assign the result to a single variable, like this

MyVariable = AnotherVariable + 3 + Age

In the code above the + operator is adding up the arguments on the right hand side of =, then the = operator assigns the result to the argument on its left: MyVariable.

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: What are assignment operators?