Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Mod for Odd/Even

Status
Not open for further replies.

stevio

Vendor
Jul 24, 2002
78
0
0
AU
All,

Trying to take variables from the command line and using modulus to check that all variables are either all odd or all even.

example of all even numbers might be

var1 = 10 var2 = 0 var3 = 4

example of all odd numbers might be

var1 = 11 var2 = 1 var3 = 3

So code to check for odd is:
Code:
if not (var1 mod 2) then
  if not (var2 = 1) then 'second variable is either 0 or 1
     if not (var3 mod 2) then
        wscript.echo "invalid - must be all odd or even"
        wscript.quit
     end if
  end if
end if

Of course when I have wscript.quit, then I exit and can't check for even numbers. But I need to halt the script

Code for checking even numbers:

Code:
if not (var1 mod 2) then
   if not(var2 = 0) then
      if not(var3 mod 2) then
         wscript.echo "invalid - must be all even or odd"
         wscript.quit
      end if
   end if
end if

Any ideas on how I can check for either condition, both of which are possible? Individually they both work.
 
I prefer slightly here with =0 or =1 explicit in the case. But...
[tt]
select case true
case ((var1 mod 2)=0) and ((var2 mod 2)=0) and ((var3 mod 2)=0)
'do something all even
case ((var1 mod 2)=1) and ((var2 mod 2)=1) and ((var3 mod 2)=1)
'do something all odd
case else
wscript.echo "invalid - must be all even or odd"
'do something like wscript.quit
end select
[/tt]
 
oddConditon = "ODD"
if not (var1 mod 2) then
if not (var2 = 1) then 'second variable is either 0 or 1
if not (var3 mod 2) then
oddCondition = "invalid - must be all odd or even"
end if
end if
end if
evenCondition = "EVEN"
if not (var1 mod 2) then
if not(var2 = 0) then
if not(var3 mod 2) then
evenCondition ="invalid - must be all even or odd"
end if
end if
end if
wscript.echo oddCondition & vbcr & evenCondition
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top