Prev | Current Page 136 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

For example, suppose you wanted to repeat a
task, but carry out a specific function for every fifth repetition. The best trick would be to create a
variable and initialize it with zero, and then every time the task repeated, you could increase the variable
by one and check its value using modulo, like so:
repeater = repeater + 1;
if (repeater % 5 == 0) myFunction();
If the value returned by this equation is zero, then you would know to execute your function. Useful,
huh?
Increment and Decrement
Very often in your code, you will use variables as a form of counting. When counting, you would often
start with a number and either increase or decrease its value by one. So, if you have a variable called
count and wish to count up, you could do the following:
count = count + 1;
Now, this is all well and good, but it does take up quite a bit of real estate. You can, however, simplify
the way this task is executed by using the increment ( ++ ) operator, which increases a value by one, or
decrement ( -- ) operator, which decreases a value by one.


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