Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 15th, 2008, 01:54 PM
Authorized User
 
Join Date: Jun 2008
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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?

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

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.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.