Prev | Current Page 137 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

To use this feature, you must append the
operator either before or after the value:
count++;
++count;
Part I: The Core Language
58
You can also use the increment and decrement operators within an expression. Here, the position of the
operator affects when the value is changed. By applying the operator after the variable, the value is not
altered until after the expression is executed, while applying the operator before the variable alters the
value before the expression is executed. Let ??™ s try this in an example. Type the following code and save it
in a file called Increment.hx :
class Increment
{
public static function main()
{
var myVal:Int = 0;
trace(myVal);
// Outputs: 0
trace(++myVal);
// Outputs: 1
trace(myVal--);
// Outputs: 1
trace(myVal);
// Outputs: 0
}
}
Once the integer was initialized, its value of 0 was simply displayed to the screen. Then, the variable was
displayed on the screen a second time while incrementing the value. As the increment operator is before
the value, the value was incremented before it was printed, so 1 was displayed.


Pages:
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149