Assignment operator

An assignment operator is a programming construct that gives a value to a variable. In NWScript, the basic (and most common) assignment operator is the equals sign ( ). There are also assignment operators that update the value of a variable, namely  ,  ,  ,  ,  ,  , and  .

For a simple assignment, the variable being assigned a value goes on the left of the operator, while the value to be assigned (an expression, possibly containing other operators) goes on the right. In the case where the value 1 is being assigned to a variable named, the following line would be used. x = 1; Having anything other than a variable to the left of the assignment operator is an error. Furthermore, the data type of the variable must match the type of expression to the right, with the one exception that an integer value can be assigned to a floating point variable (the conversion will be automatically added; however an explicit conversion may be better style at times). A simple assignment can also be used to initialize a variable, as in int x = 1; which simultaneously declares  to be a variable of type integer and gives that variable the value 1.

The assignment operators that update the value of a variable are shorthand notation for using an arithmetic operator or a bitwise operator. Specifically, the assignment x += 2*cos(45); is equivalent to x = x + (2*cos(45)); and similarly for the other operators. (The  operator can be used as shorthand for either numeric addition or string concatenation.) This allows for less text in the script, which can make the script more readable, at least for those familiar with these operators.

A quirk of assignment operators, one that can confuse those new to programming, is that they are operators, and so they do have a value. The value of an assignment operator is the value assigned to the variable. This allows the value 2 to be assigned to the variables  and   with a line like the following. x = (y = 2); This is not a commonly used feature, at least not intentionally. It is more common to see this feature used unintentionally as part of a conditional expression, where the single equal sign was intended to be a test for equality.