Even or Odd function in VBScript?

# Thread: Even or Odd function in VBScript?

1. dw
Junior Member
Join Date
Dec 1969
Posts
19

## Even or Odd function in VBScript?

Does anyone know of a function, or the like, that would return odd or even for an integer? I know a work-around, but I&#039m just curious. Thanks!

2. KPW Guest

## RE: Even or Odd function in VBScript?

I don&#039t know if I quite understand your question, but the Mod operator will help you determine whether a number is odd or even.<BR><BR>&#060;%<BR><BR>If 2 Mod 2 = 0 then<BR> &#039 2 is even<BR>Else<BR> &#039 2 is odd<BR>End If<BR>%&#062;<BR><BR>Okay, that&#039s a STUPID example, but I think you understand how it would work with variables. Basically, Mod returns the remainder of a division operation. Perhaps a better illustration would have been:<BR><BR>&#060;%<BR><BR>If 4 Mod 2 = 0 then<BR>...<BR>End If<BR><BR>If 3 Mod 2 = 0 then<BR>...<BR>End if<BR>%&#062;

3. dw
Junior Member
Join Date
Dec 1969
Posts
19

## RE: Even or Odd function in VBScript?

I ended up using Mod. My question is whether there is a built-in function like a boolean function "IsOdd" or "IsEven". Actually, I know those IsOdd and IsEven don&#039t exist.

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Even or Odd function in VBScript?

&nbsp;<BR>No built in functions, but you could easily create your own.<BR><BR>Mod works, but AND is faster:<BR><BR>If someNumber AND 1 &#060;&#062; 0 Then <BR>&nbsp;&nbsp;&nbsp;&nbsp;... odd ..<BR>Else<BR>&nbsp;&nbsp;&nbsp;&nbsp;... even ...<BR>End If<BR><BR>Caution: if you use AND with a non-number, then you end up getting a logical AND instead of a bitwise one!<BR><BR>So a function:<BR><BR>Function IsOdd( num )<BR>&nbsp;&nbsp;&nbsp;&nbsp;IsOdd = ( ( num AND 1 ) &#060;&#062; 0 )<BR>End Function<BR><BR>Function IsEven( num )<BR>&nbsp;&nbsp;&nbsp;&nbsp;IsEven = ( ( num AND 1 ) = 0 )<BR>End Function<BR><BR>As you can see, there&#039s not much reason not to simply code the equivalent of the function in-line. But whatever...<BR><BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•