Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / MS VBScript / July 2008



Tip: Looking for answers? Try searching our database.

Multiple IF Conditions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
preet - 03 Jul 2008 22:43 GMT
Is there a better way to handle multiple IF Conditions like

IF condition1 AND condition2 AND condition3 AND .... THEN

code

END IF

Now suppose i have 30 to 40 conditions to test, how do i handle this
snippet in an easier manner.

Guide

--------------------------
http://www.eecpindia.com
http://forex.eecpindia.com
Richard Mueller [MVP] - 04 Jul 2008 03:57 GMT
> Is there a better way to handle multiple IF Conditions like
>
[quoted text clipped - 8 lines]
>
> Guide

I don't think there is a better way. In theory if you know that one or a few
of the conditions are more likely to be false, the code could be more
efficient if you nested the If statements, testing the ones likely to be
false first. Then the remaining conditions are seldom evaluated. For
example, if condition1, condition2, and condition3 are more likely to be
false than the others:

If condition1 Then
   If condition2 Then
       If condition 3 Then
           If condition4 And condition5 And condition6 Then
               ' ... code.
           End If
       End If
   End If
End If

Even here I doubt you could tell the difference, even with 40 conditions.

Signature

Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

Al Dunbar - 05 Jul 2008 06:24 GMT
>> Is there a better way to handle multiple IF Conditions like
>>
[quoted text clipped - 27 lines]
>
> Even here I doubt you could tell the difference, even with 40 conditions.

There is a way to reduce the nesting from any number of levels to one:

   a = "a"
   b = "b"
   c = "ZZZ"

   do
       if not a = "a" then exit do
       if not b = "b" then exit do
       if not c = "c" then exit do

       wscript.echo "test1: all conditions are true"
   loop until true

   c = "c"

   do
       if not a = "a" then exit do
       if not b = "b" then exit do
       if not c = "c" then exit do

       wscript.echo "test2: all conditions are true"
   loop until true

If you need an "else" clause, then:

   a = "a"
   b = "b"
   c = "ZZZ"

   do
       test1 = false
       if not a = "a" then exit do
       if not b = "b" then exit do
       if not c = "c" then exit do
       test1 = true
   loop until true

   if test1 then
       wscript.echo "all conditions were true"
   else
       wscript.echo "at least one conditions was false"
   end if

/Al
Dr J R Stockton - 05 Jul 2008 16:32 GMT
In microsoft.public.scripting.vbscript message <eBtb1GY3IHA.3480@TK2MSFT
NGP03.phx.gbl>, Thu, 3 Jul 2008 21:57:19, "Richard Mueller [MVP]"
<rlmueller-nospam@ameritech.nospam.net> posted:

>I don't think there is a better way. In theory if you know that one or a few
>of the conditions are more likely to be false, the code could be more
>efficient if you nested the If statements, testing the ones likely to be
>false first.

Not necessarily; some conditions take more work than others.

Signature

(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk  Turnpike v6.05  MIME.
Web  <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

mayayana - 04 Jul 2008 04:07 GMT
There isn't much to go on in your question. Assuming that
there's no better design you can come up with than 30+
conditions ... you might be able to use a Select Case. But if
the test is all "And"s, and you can't break it down into
subsets somehow, a separate function might be the cleanest
way:

If ChecksOut(var) = True then DoSomething

Function ChecksOut(varIn)
 ChecksOut = False
if var = a then exit function
if var = b then exit function
...etc...
ChecksOut = True
End Function

 Each condition on it's own line. If any fails the
function quits. If none fails it returns True.

> Is there a better way to handle multiple IF Conditions like
>
[quoted text clipped - 14 lines]
>
> *** Sent via Developersdex http://www.developersdex.com ***
preet - 04 Jul 2008 04:58 GMT
hmmm, the function part sounds interesting.

should help cleanup the code

thanks Richard  and mayayana for your valuable inputs.

--------------------------
http://www.eecpindia.com
http://forex.eecpindia.com
ekkehard.horner - 04 Jul 2008 09:59 GMT
preet schrieb:
> Is there a better way to handle multiple IF Conditions like
>
[quoted text clipped - 4 lines]
> Now suppose i have 30 to 40 conditions to test, how do i handle this
> snippet in an easier manner.

Use "Select Case False" instead of "If c1 And c2 ... Then" and
"Select Case True" instead of "If c1 Or c2 .. Then":

  Dim aTests : aTests = Array( 11, 12, 2 )
  Dim nTest, sContext, sResult
  For Each nTest In aTests
      WScript.Echo "Testing", nTest
      sContext = "IF AND"
      WScript.Echo " Context", sContext
      If     checkModulo( nTest, 2 ) _
         And checkModulo( nTest, 3 ) _
         And checkModulo( nTest, 4 ) _
      Then
         WScript.Echo " YES"
      Else
         WScript.Echo " NO"
      End If

      sContext = "SELECT FALSE (AND)"
      WScript.Echo " Context", sContext
      sResult = " NO"
      Select Case False
        Case checkModulo( nTest, 2 )
        Case checkModulo( nTest, 3 )
        Case checkModulo( nTest, 4 )
        Case Else
          sResult = " YES"
      End Select
      WScript.Echo sResult

      sContext = "IF OR"
      WScript.Echo " Context", sContext
      If    checkModulo( nTest, 2 ) _
         Or checkModulo( nTest, 3 ) _
         Or checkModulo( nTest, 4 ) _
      Then
         WScript.Echo " YES"
      Else
         WScript.Echo " NO"
      End If

      sContext = "SELECT TRUE (OR)"
      WScript.Echo " Context", sContext
      sResult = " YES"
      Select Case True
        Case checkModulo( nTest, 2 )
        Case checkModulo( nTest, 3 )
        Case checkModulo( nTest, 4 )
        Case Else
          sResult = " NO"
      End Select
      WScript.Echo sResult

  Next

  Function checkModulo( n1, n2 )
    checkModulo = 0 = (n1 Mod n2)
    WScript.Echo "  checkModulo(", n1, ",", n2, ") =>", CStr( checkModulo )
  End Function

output:

=== checkConditions2: ways to check conditions (2) ===
Testing 11
 Context IF AND
  checkModulo( 11 , 2 ) => Falsch
  checkModulo( 11 , 3 ) => Falsch
  checkModulo( 11 , 4 ) => Falsch
 NO
 Context SELECT FALSE (AND)
  checkModulo( 11 , 2 ) => Falsch
 NO
 Context IF OR
  checkModulo( 11 , 2 ) => Falsch
  checkModulo( 11 , 3 ) => Falsch
  checkModulo( 11 , 4 ) => Falsch
 NO
 Context SELECT True (OR)
  checkModulo( 11 , 2 ) => Falsch
  checkModulo( 11 , 3 ) => Falsch
  checkModulo( 11 , 4 ) => Falsch
 NO
Testing 12
 Context IF AND
  checkModulo( 12 , 2 ) => Wahr
  checkModulo( 12 , 3 ) => Wahr
  checkModulo( 12 , 4 ) => Wahr
 YES
 Context SELECT FALSE (AND)
  checkModulo( 12 , 2 ) => Wahr
  checkModulo( 12 , 3 ) => Wahr
  checkModulo( 12 , 4 ) => Wahr
 YES
 Context IF OR
  checkModulo( 12 , 2 ) => Wahr
  checkModulo( 12 , 3 ) => Wahr
  checkModulo( 12 , 4 ) => Wahr
 YES
 Context SELECT True (OR)
  checkModulo( 12 , 2 ) => Wahr
 YES
Testing 2
 Context IF AND
  checkModulo( 2 , 2 ) => Wahr
  checkModulo( 2 , 3 ) => Falsch
  checkModulo( 2 , 4 ) => Falsch
 NO
 Context SELECT FALSE (AND)
  checkModulo( 2 , 2 ) => Wahr
  checkModulo( 2 , 3 ) => Falsch
 NO
 Context IF OR
  checkModulo( 2 , 2 ) => Wahr
  checkModulo( 2 , 3 ) => Falsch
  checkModulo( 2 , 4 ) => Falsch
 YES
 Context SELECT True (OR)
  checkModulo( 2 , 2 ) => Wahr
 YES
=== checkConditions2: 0 done (00:00:00) ==============

As you can see, this technique brings lazy evaluation of conditions
to VBScript.

Some more examples for "Select Case":

  Dim aTests : aTests = Array( "lazy", "busy", "lary" )
  Dim nI, nJ
  For nI = 0 To UBound( aTests )
      For nJ = 0 To UBound( aTests )
          WScript.Echo "Is", aTests( nI ), "==", aTests( nJ )
          Select Case False
            Case Mid( aTests( nI ), 1, 1 ) = Mid( aTests( nJ ), 1, 1 )
              WScript.Echo " 1 <>"
            Case Mid( aTests( nI ), 2, 1 ) = Mid( aTests( nJ ), 2, 1 )
              WScript.Echo " 2 <>"
            Case Mid( aTests( nI ), 3, 1 ) = Mid( aTests( nJ ), 3, 1 )
              WScript.Echo " 3 <>"
            Case Else
              WScript.Echo "YES"
          End Select
      Next
  Next
  WScript.Echo "----------"

  For nI = 0 To UBound( aTests )
      WScript.StdOut.Write aTests( nI )
      Select Case True
        Case Left(       aTests( nI ),    1 ) <> "l"
          WScript.Echo " 1 not l"
'       Case Not( Mid(   aTests( nI ), 3, 1 ) =  "z" _
'             Or  Mid(   aTests( nI ), 3, 1 ) =  "r" )
        Case 0 = InStr( "zr", Mid(   aTests( nI ), 3, 1 ) )
          WScript.Echo " 3 not z|r"
        Case Right( aTests( nI ),    1 ) <> "y"
          WScript.Echo " last not y"
        Case Else
          WScript.Echo " l.[zr]y"
      End Select
  Next
  WScript.Echo "----------"

  Dim sOrT : sOrT = "busy"
  Dim sType
  For nI = 1 To Len( sOrT )
      Select Case Mid( sOrT, nI, 1 )
        Case "b", "s"
          sType = "consonant"
        Case "a", "u"
          sType = "vowel"
        Case "y"
          sType = "semivowel"
      End Select
      WScript.Echo Mid( sOrT, nI, 1 ), sType
  Next
  WScript.Echo "----------"

output:

=== checkConditions: ways to check conditions ====
Is lazy == lazy
YES
Is lazy == busy
 1 <>
Is lazy == lary
 3 <>
Is busy == lazy
 1 <>
Is busy == busy
YES
Is busy == lary
 1 <>
Is lary == lazy
 3 <>
Is lary == busy
 1 <>
Is lary == lary
YES
----------
lazy l.[zr]y
busy 1 not l
lary l.[zr]y
----------
b consonant
u vowel
s consonant
y semivowel
----------
=== checkConditions: 0 done (00:00:00) ===========
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.