Wrox Programmer Forums IF statements with even or odd numbers
 Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
 Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersâ€™ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
July 15th, 2008, 01:54 PM
 Authorized User Join Date: Jun 2008 Location: , , . Posts: 33 Thanks: 2 Thanked 0 Times in 0 Posts
IF statements with even or odd numbers

Is there a way to say:

If x is odd then
(do what I want it to do)
Else
(do the other thing I want it to do)

If I put it in this form it will work:

If x=1 or x=3 or x=5 then....

But I don't want to do that for hundreds of numbers I just want it to know to do one thing for even numbers and another for odd.

Any ideas?

July 15th, 2008, 02:19 PM
 Friend of Wrox Join Date: Jun 2008 Location: Snohomish, WA, USA Posts: 1,649 Thanks: 3 Thanked 141 Times in 140 Posts

Several ways:

If (x MOD 2) = 1 Then
' x is odd
Else
' x is even
End If

Assuming x is an INT or LONG value, you could also do

If (x AND 1) = 1 Then
' x is odd
Else
' x is even
End If

You could assure that x is integral by applying CLNG() function.

If (CLNG(x) AND 1) = 1 Then

There are other ways, but those are the most efficient.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is OffTrackbacks are Off Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Odd InputBox error Archdeacon VBScript 3 June 15th, 2008 10:50 PM Odd form behaviour jeremy1048 Access 0 April 25th, 2008 08:13 AM Rather odd web-project oldmainframehack Javascript How-To 0 August 8th, 2007 09:03 AM Need help with XSLFO - even/odd pages nyctechwriter XSLT 0 August 23rd, 2006 11:01 AM Odd Grouping Request kilika SQL Server 2000 2 July 10th, 2003 02:36 AM

All times are GMT -4. The time now is 11:58 AM.