Previous section To contents Next section

5.6 The assignment operators

There is really only one assignment operator, but it can be combined with lots of other operators to make the code shorter. An assignment looks like this:
variable = expression;
The variable can be a local variable, a global variable or an index in an array, object, multiset or mapping. This will of course set the value stored in variable to expression. Note that the above is also an expression which returns the value of the expression. This can be used in some interesting ways:
variable1 = variable2 = 1; // Assign 1 to both variables
variable1 =(variable2 = 1); // Same as above

// Write the value of the expression, if any
if(variable = expression)
    write(variable);
Using assignments like this can however be confusing to novice users, or users who come from a Pascal or Basic background. Especially the if statement can be mistaken for if(variable == expression) which would mean something completely different. As I mentioned earlier, the assignment operator can be combined with another operator to form operators that modify the contents of a variable instead of just assigning it. Here is a list of all the combinations:

Syntax Same as Function
variable += expression  variable = variable + expression Add expression to variable
variable -= expression  variable = variable - expression Subtract expression from variable
variable *= expression  variable = variable * expression Multiply variable with expression
variable /= expression  variable = variable / expression Divide variable by expression
variable %= expression  variable = variable % expression Modulo variable by expression
variable <<= expression  variable = variable << expression Shift variable expression bits left
variable >>= expression  variable = variable >> expression Shift variable expression bits right
variable |= expression  variable = variable | expression Or variable with expression
variable &= expression  variable = variable & expression And variable with expression
variable ^= expression  variable = variable ^ expression Xor variable with expression

In all of the above expressions variable can actually be any of type of assignable values. Assignable values are also known as lvalues and here is a list of lvalues:

Lvalue type Syntax Valid assignment type
a local or global variable   identifier  same as variable
an element in an array   array [ int ] any type
elements in elements in an array   array [ string ] any type
This is like map(arr, `[]=,string_indexing_element, assignment_element)
an element in an string   string [ int ] integer
an element in a mapping   mapping[mixed] or mapping->identifier  any type
an element in a multiset   multiset[mixed] or multiset->identifier  true / false
a variable in an object   object[string] or object->identifier  same type as named variable
a list of lvalues   [ lvalue, lvalue ]   an array, first value in the array will be assigned to the first lvalue in the list, second value in the array to the second value in the list etc.


Previous section To contents Next section