Skip to main content
Inspiring
November 10, 2008
Question

Detecting even and odd numbers

  • November 10, 2008
  • 3 replies
  • 748 views
Hi, I'm drawing a series of rectangles using AS3, and need every alternate one to be lighter... I'm using a switch statement currently, with a variable counting the number of rectangles and case 1, case 2, case 3, etc.

Is there a formula or some way of telling whether there is an even or odd number of rectangles so my switch statement can just be case even:, case odd: ? The way I have it is fine for 10 or so rectangles but I'd rather make it more expandable.

Would appreciate any suggestions
This topic has been closed for replies.

3 replies

Inspiring
November 10, 2008
Ah, good to know, thanks
Inspiring
November 10, 2008
Thanks, that works! Not sure how though... could you explain?
Inspiring
November 10, 2008
% is a modulo operator that finds a remainder of a division. 9%2 = 1, 9%4 = 1, but 9%3 = 0.

In the case of odd/even numbers one can look at it as that an even number that leaves no remainder when divided by 2 and odd number is a number that leaves remaining 1 when divided by 2.
webqaflash
Inspiring
November 10, 2008
If you got the number of rectangle then you can use % with 2.If it is an odd number the result is 1 and it is even then result will be 0;