From: preet on
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


*** Sent via Developersdex http://www.developersdex.com ***
From: Richard Mueller [MVP] on

"preet" <preetkanwaljit> wrote in message
news:eI9RTXV3IHA.5060(a)TK2MSFTNGP02.phx.gbl...
> 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

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.

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


From: mayayana on

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
>
>
> 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
>
>
> *** Sent via Developersdex http://www.developersdex.com ***


From: preet on
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


*** Sent via Developersdex http://www.developersdex.com ***
From: ekkehard.horner on
preet schrieb:
> 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.

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) ===========