if a number is even... if it's odd....

Simple question for one of you code gurus out there:

I want an if-then statement that will allow me to test if a given number is even, for example:

for (i=0; i<1000; i++)
{
if (i IS EVEN) { Do something }
}

Thanks in advance!

A number is "even" when you can divide it by two and have zero remainder.

if ( (i % 2) == 0) { do_something(); }

A number is "even" if the least significant bit is zero.

if ( (i & 0x01) == 0) { do_something(); }
1 Like

Perfect! Thanks!

Also "0" is false, "anything but 0" is true, so:

if (i % 2) { // do something odd }

should also work.