optimize If conditional expression?
"JLGWhiz" wrote:
Well, the options appear to be using If...ElseIf...Else...End If
The "problem" is: it would have to be of the form:
If condition1 Then
Goto doit
Else If condition 2 Then
Goto doit
Else If condition3 Then
doit:
...statements...
End If
Not really a problem. Just undesirable. Also, "...statements..." cannot be
procedurized easily.
or Select Case statements.
I'm getting used to that form, as I have been using it in the interim. Very
malleable: easy to add additional conditions.
I believe in the Or statement that if the first part of
the statement is true, it does not check the second part
Since I asserted "that will evaluate both Or operands even if the first one
is true", you might think that I tested this, or at the very least, it might
motivate you to test it yourself.
Try the following....
Sub doit1()
If tryit(1, True) Or tryit(2, True) Then
tryit 0, False
End If
End Sub
Sub doit2()
Select Case True
Case tryit(1, True), tryit(2, True)
tryit 0, False
End Select
End Sub
Private Function tryit(n As Integer, t As Boolean) As Boolean
MsgBox "tryit " & n & " " & t
tryit = t
End Function
Remember that I am using Excel and VBA.
----- original message -----
"JLGWhiz" wrote in message
...
Well, the options appear to be using If...ElseIf...Else...End If
or Select Case statements.
As a side note, I believe in the Or statement that if the first part of
the statement is true, it does not check the second part. It does check
both conditions in an And statement because they both have to be true for
the statement to be true.
But, without seeing your complex problem, that's about it.
"Joe User" <joeu2004 wrote in message
...
I want to write:
If Left(s,2) = "Mr" Or Left(s,2) = "Ms" Then
...statements...
End If
But that will evaluate both Or operands even if the first one is true.
Is there a more efficient way to implement this?
Note: That example is a simplification. In actual practice, the
conditional expressions are more complicated. So don't try to optimize
the particular example. Optimize the paradigm.
My best:
doit = (Left(s,2) = "Mr")
If Not doit Then doit = (Left(s,2) = "Ms")
If doit Then
...statements...
End If
But that gets a little tedious. Alternatively:
Select Case True
Case Left(s,2) = "Mr", Left(s,2) = "Ms"
...statements....
End Select
But that seems a bit convoluted.
Am I overlooking the obvious?
|