FANDOM


(someone clean it after me, I tried to add some relevant informations but well im not good in grammar, thanks)
Line 18: Line 18:
 
x = (y = 2);
 
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 (<code>[[comparison operator|==]]</code>).
 
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 (<code>[[comparison operator|==]]</code>).
  +
The operators ++ and -- can be used only with integer types and they will either add a 1 or substract a 1. Both assignments are special as they can be used inside another assignment or parameter in function. Also they can be used "before" and "after" like int m = ++n; or int m = n++; The difference is that the first expression assign into m the value of n+1 but second statement assigns into m only value of n and the +1 will be added into n after the expression in which it was used.
 
[[category:operators]]
 
[[category:operators]]

Revision as of 02:53, October 12, 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 (==). The operators ++ and -- can be used only with integer types and they will either add a 1 or substract a 1. Both assignments are special as they can be used inside another assignment or parameter in function. Also they can be used "before" and "after" like int m = ++n; or int m = n++; The difference is that the first expression assign into m the value of n+1 but second statement assigns into m only value of n and the +1 will be added into n after the expression in which it was used.

Community content is available under CC-BY-SA unless otherwise noted.