Home : Actionscript 3.0 : AS3 Quick Tips: How to check if a number is odd or even
AS3 Quick Tips: How to check if a number is odd or even
By Bill Nunney ⋅ October 11, 2008 ⋅ Post a comment
At some point in a developers life you find the need to check if a number is odd or even. Whether it be to make a staggered output (alternating row colours) or a more complicated calculation. For this purpose there is a special operator called “Modulo” (admit it, it sounds cool).
Modulo is represented by the percent symbol “%”. Modulo’s behavior is to return the remainder of the left expression divided by the right expression.
Example:
var myModulo:Number = 3 % 2; trace(myModulo);
The above example would output the remainder of the calculation 3 / 2 which is ‘1′.
In the following example i create colour alternating lines using a for loop and Modulo in an if statement:
Any questions or requests please leave a comment











Tags:
Great tip - exactly what I was in need of for a project.
Thanks a bunch!