AS3 Quick Tips: How to check if a number is odd or even

Flash CS3 Professional

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 :)

2 Comments to “AS3 Quick Tips: How to check if a number is odd or even”

  1. Jason 11 November 2008 at 4:46 am #

    Great tip – exactly what I was in need of for a project.

    Thanks a bunch!

  2. Jon 8 March 2010 at 3:49 pm #

    I keep coming back to this post ever 6 months, forgetting how to do this, thanks for this!


Leave a Reply