Assignment operators
From ZCWiki
Assignment operators assign a value to a variable. The most familiar assignment operator is the equals sign "=", however there are other assignment operators that act as "shortcuts" for several of the mathematical operators.
Contents |
General Syntax and Usage
The basic assignment operator assigns the value of the expression on the right-hand side of the operator to the variable on the left-hand side. For instance,
x = 12 + 3;
assigns the value of 12 + 3 (which is of course 15) to the variable x. The complexity of the expression on the right-hand side of the operator is practically unlimited. The final value of the right-hand expression should be of the same data type as the variable on the left-hand side of the operator. For example, if the expression on the right ends up being a boolean true or false while the variable on the left is an integer or float, you may get unexpected results - or your script won't compile at all. See typecasting for additional details.
Due to the nature of the assignment operator, the left-hand operand must be a variable or an editable property (such as Link->HP, Link's hitpoints). You cannot use the assignment operator in statements like,
12 + 3 = x;
or
x + 2 = 3 - y;
These are invalid and will not compile.
Note, however, that the variable on the left-hand side of the operator may also appear in the right-hand expression. Keep in mind that the right-hand side expression is always evaluated first, and then the result is stored in the variable on the left-hand side. For instance, the two statements
x = 10; x = x + 10;
are executed as follows:
- in the first statement, a value of 10 is assigned to x.
- in the second statment, 10 is added to x for a final value of 20, and then that value of 20 is stored back into x.
Variants of the Assignment Operator
Often times you will need to modify the value of a variable (or built-in property, like Link's hitpoints) and store the result back into the same variable or property. Using just the basic assignment operator, you would write such an operation as follows:
// Add 16 hitpoints to Link's hitpoints.
Link->HP = Link->HP + 16;
Variants of the assignment operator can shorten such operations. In the previous example, the Additive Assignment Operator could be used as follows:
// Add 16 hitpoints to Link's hitpoints.
Link->HP += 16;
Examples of the assignment operator variants are given below, but in all cases the variant is simply the desired operator (+, -, *, etc.) followed directly by the equals sign.
Additive Assignment Operator
The additive assignment operator adds the expression on the right-hand side of the operator to the value of the variable on the left-hand side. For example,
Link->HP += 16;
is the same as
Link->HP = Link->HP + 16;
Subtractive Assignment Operator
The subtractive assignment operator subracts the expression on the right-hand side of the operator from the value of the variable on the left-hand side. For example,
Link->HP -= 16;
is the same as
Link->HP = Link->HP - 16;
Multiplicative Assignment Operator
The multiplicative assignment operator multiplies the the value of the variable on the left-hand side by the expression on the right-hand side. For example,
Link->HP *= 2;
is the same as
Link->HP = Link->HP * 2;
Divisive Assignment Operator
The divisive assignment operator divides the the value of the variable on the left-hand side by the expression on the right-hand side. Bare in mind that division by 0 is undefined. For example,
Link->HP /= 2;
is the same as
Link->HP = Link->HP / 2;
