NWNWiki
NWNWiki
3,718
pages
("+=" can also be the string operator)
No edit summary
Line 1: Line 1:
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 ('''<code>=</code>'''). There are also assignment operators that update the value of a variable, namely '''<code>+=</code>''', '''<code>-=</code>''', '''<code>*=</code>''', '''<code>/=</code>''', '''<code>%=</code>''', '''<code>|=</code>''', and '''<code>&=</code>'''.
+
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 ('''<code>=</code>'''). There are also assignment operators that update the value of a variable, namely '''<code>++</code>''', '''<code>--</code>''', '''<code>+=</code>''', '''<code>-=</code>''', '''<code>*=</code>''', '''<code>/=</code>''', '''<code>%=</code>''', '''<code>|=</code>''', and '''<code>&=</code>'''.
   
 
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 <code>x</code>, the following line would be used.
 
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 <code>x</code>, the following line would be used.

Revision as of 00:44, 12 October 2011

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 x, 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 x 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, string, or 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 x and y 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 (==).