"Lauren Quantrell" <la*************@hotmail.com> wrote in
news:11**********************@g14g2000cwa.googlegr oups.com:
Is there any speed/resource advantage/disadvantage in using
Select Case x
Case 1
Case 2
etc. many more cases...
End Select
VS.
If x Then
ElseIf x+1 Then
ElseIf x+2 Then
etc. many more ElseIfs...
End If
Others have given you good answers, and to your specific question
about the nesting of multiple levels of CASE SELECT.
There's another issue:
Sometimes a CASE SELECT is *not* equvalent.
If/Then/Else is processed in sequence, and each control line (the
If/Then, the ElseIf/Then and the Else lines) is processed
independently. That means you have 2 or more tests that you can run,
and they don't have have to be run on the same expression.
CASE SELECT operates on a single expression, usually testing a
single value.
CASE SELECT can do more than testing equality, though:
CASE SELECT lngVariable
CASE <0
[do whatever]
CASE 0
[do whatever]
CASE >0
[do whatever]
END SELECT
I see no reason why that is superior to (I'll explain the odd logic
later):
If lngVariable < 0 THen
[do whatever]
Else lngVariable = 0 Then
[do whatever]
Else
[do whatever]
End If
Now, I don't remember the details on this, but I believe CASE SELECT
tests in order, down the list, so you're going to test 3 values for
numbers >0, 2 for 0 and 1 for <0. With the If/Then there's only a
maximum of 2 comparisons, as the last two possibilities are handled
by a match or a non-match.
You could do the same with:
CASE SELECT lngVariable
CASE <0
[do whatever]
CASE 0
[do whatever]
CASE ELSE
[do whatever]
END SELECT
Of course, the natural order for a CASE SELECT is ordered for
numbers or alphabitical for strings (makes it easier to read),
though you may choose to use a different order if you have a
non-random distribution of values.
In any event, testing for equality with 0 is probably going to
happen less often than <> 0, so I'd leave that as the last ELSE in
either structure. So, that would give you:
CASE SELECT lngVariable
CASE <0
[do whatever]
CASE >0
[do whatever]
CASE ELSE
[do whatever]
END SELECT
And:
If lngVariable < 0 THen
[do whatever]
Else lngVariable > 0 Then
[do whatever]
Else
[do whatever]
End If
Now, that's one set of issues.
THe other is that you may be doing a Boolean test on more than one
expression. In that case, CASE ELSE becomes complicated. Your
nesting example is exactly that. It could have been expressed as a
Boolean expression of ((x = 1) and (y = 1)), though your particular
example doesn't need to be expressed that way.
The point is that you may have non-symmetrical tests. You may do
some completely different branch of code for the NOT case of such a
set of tests. In that case, If/Then/Else is probably better, or a
mix of it with CASE/SELECT at the lower level.
So, balanced trees can use either structure, with CASE SELECT being
better, while unbalanced trees often are better suited to
If/Then/Else.
Hope that nakes sense!
--
David W. Fenton
http://www.bway.net/~dfenton
dfenton at bway dot net
http://www.bway.net/~dfassoc