Language > Operators > Assignment and Compound Assignment

# Assignment and Compound Assignment

The assignment statement stores a value in a variable. Compound assignment combines assignment with another operator.

Operator | Description | Examples
= | Assignment The value of the expression on the right hand side of the equal sign is stored in the variable, subscript element, or range on the left side. The old value of the variable, if any, is discarded, and the value of the expression is stored in the variable. The expression on the right side can be of any type or structure. For more information on assignment involving arrays and ranges, see Overview of Arrays. | Simple assignment examples: A = 5 Assigns 5 to variable A: B='Hello World' Assign “Hello World” to variable B: name = 'Mary' The variable `name` becomes a scalar string variable. arr = FLTARR(100) Make `arr` a 100-element, floating-point array. arr = arr[50:*] Discard points 0 to 49 of `arr`. It is now a 50-element array.
`op`= | Compound Assignment where op is one of the following operators: ##, #, *, +, -, /, <, >, ^, AND, EQ, GE, GT, LE, LT, MOD, NE, OR, XOR Provides succinct syntax for expressions in which the same variable would otherwise be present on both sides of the equal sign. See Compound Assignment Operators for details. | Applies the specified operation to the target variable “in place,” without making a copy of the variable. For example, A += 5 adds 5 to the value of the variable A. A op= expression is equivalent to: A = TEMPORARY(A) op (expression) The following statements both add 100 to current value of A: A = A + 100 A += 100

## Compound Assignment Operators

In addition to the standard assignment statement, IDL versions beginning with 6.0 support the following compound assignment operators:

##= | #= | *= | += | -=
/= | <= | >= | AND= | EQ=
GE= | GT= | LE= | LT= | MOD=
NE= | OR= | XOR= | ^= |

See op= in previous table for examples.

Note: If one or both of the operands are objects, the operator may be overloaded. See Object Operator Overloading Overview for more information.

These compound operators combine assignment with another operator. A statement such as:

A op= expression

where op is an IDL operator that can be combined with the assignment operator to form one of the above-listed compound operators, and expression is any IDL expression, produces the same result as the statement:

A = A op (expression)

The difference is that the statement using the compound operator makes more efficient use of memory, because it performs the operation on the target variable A in place. In contrast, the statement using the simple operators makes a copy of the variable A, performs the operation on the copy, and then assigns the resulting value back to A, temporarily using extra memory.

Note that the statement:

A op= expression

is identical to the IDL statement:

A = TEMPORARY(A) op (expression)

which uses the TEMPORARY function to avoid making a copy of the variable A. While there is no efficiency benefit to using the compound operator rather than the TEMPORARY function, the compound operator allows you to write the same statement more succinctly.

### Compound Operators and Whitespace

When using the compound operators that include an operator referenced by a keyword rather than a symbol (AND=, for example), you must be careful to use whitespace between the operator and the target variable. Without appropriate whitespace, the result will not be what you expect. Consider the difference between these two statements:

AAND= 23

A AND= 23

The first statement assigns the value 23 to a variable named AAND. The second statement performs the AND operation between A and 23, storing the result back into the variable A.

Compound operators that do not involve IDL keywords (+=, for example) do not require whitespace in order to be properly parsed by IDL, although such whitespace is recommended for code readability. That is, the statements

A+= 23

A += 23

are identical, but the latter is more readable.