Link to home
Start Free TrialLog in
Avatar of rhinez0rz
rhinez0rz

asked on

Visual Basic 6 - "Select Case" VS "Elseif"

Which is better in terms of performance & memory usage, which is
higher performance and less memory usage?

"Select Case" VS "Elseif"

The language I am asking about is VB6.
ASKER CERTIFIED SOLUTION
Avatar of List244
List244

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of List244
List244

One thing to note however, is the following:

A switch statement only needs to evaluate once, while if you have multiple if statments
asking the same thing, they have to evaluate more than once. For example:

Select Case 1 + 3
    Case 1:
        MsgBox "1"
    Case 2:
        MsgBox "2"
    Case 3:
        MsgBox "3"
    Case 4:
        MsgBox "4"
    Case Default:
        MsgBox "Default"
End Select
If 1 + 3 = 1 Then
    MsgBox "1"
ElseIf 1 + 3 = 2 Then
    MsgBox "2"
ElseIf 1 + 3 = 3 Then
    MsgBox "3"
ElseIf 1 + 3 = 4 Then
    MsgBox "4"
End If

The select case only had to evaluate 1+3 once while the if had to do it 4 times.
Select Case will be preferred when ther are more than one action possible per condition, whereas ElseIf is per one action at a time. Example:

Select Case SomeCondition
    Case 2, 4, 6 To 9, 12
        'do your stuff
    Case 1, 3, 5
        'do the other stuff
    Case Else
        'take vacation
End Select

This you cannot do with ElseIf, so the answer is more driven by the number of expecetd results and less by memory usage and performace.

S
Shauli, that is not correct, like I stated earlier, it is really about which is cleanest.  

If temp = 1 Or temp = 4 Or (temp >= 6 & temp <= 9) Or temp = 12 Then
    MsgBox temp
ElseIf temp = 1 Or temp = 3 Or temp = 5 Then
    MsgBox temp
Else
    MsgBox temp
End If

That is an equivalent if-else to your case statement.  It isn't quite as clean, but it works.  And like
I stated earlier, it has to check the value of temp much more than would be needed with a select
statement.
That is correct. Of course your sample works, but it is not as efficient. As MSDN put it:

"...Tip   Select Case may be more useful when evaluating a single expression that has several possible actions...."

S
Shauli, I have already pointed out that using the select case is more efficient in the way that it evaluates less.  I was
simply saying that your example which claimed the select case could not be done with an if-else, could.
You can always travel from Philadelphia to New York through Timbuktu. Can it be done? Sure.

S
Avatar of rhinez0rz

ASKER

I meant to give credit for this answer:

Select Case will be preferred when ther are more than one action possible per condition, whereas ElseIf is per one action at a time. Example:

Select Case SomeCondition
    Case 2, 4, 6 To 9, 12
        'do your stuff
    Case 1, 3, 5
        'do the other stuff
    Case Else
        'take vacation
End Select

This you cannot do with ElseIf, so the answer is more driven by the number of expecetd results and less by memory usage and performace.

S


--------

Could you fix this? Thanks
Rhinez0rz, that answer was incorrect.  That can be done with an if-else as I have showed you.
Also, if you read my previous comments, you will see that I already said that select would be
more efficient with multiple cases.  Only, I actually told you in addition to that, that it is because
of the fact that it must evaluate more times.  What was wrong with my answer?