Select...Case & If...Else - are there any advantages/disadvantages of the former over the latter or vice-versa?
Printable View
Select...Case & If...Else - are there any advantages/disadvantages of the former over the latter or vice-versa?
I believe the perceived efficiency issue is that in a SELECT/CASE with lots of CASE statements that it will jump to the END SELECT after the active CASE is met.
There is obvious syntax differences - the SELECT/CASE simply looks much better visually with a dozen options.
There is a kind of "implied hierarchy" to IF/THEN/ELSe/IF...
The same happens in If...Else statements as well; as soon as the condition is satisfied, VB runtime will jump to the End If line.......Quote:
I believe the perceived efficiency issue is that in a SELECT/CASE with lots of CASE statements that it will jump to the END SELECT after the active CASE is met.
Switch or Select Statements are more standard in a situation where you have a number of possible cases. There isn't really a performance difference that you would ever notice... it's just one of those things where you have a normal way to write code.
Select case is more efficient.. as stated... and easier to use in some cases
Like:
VB Code:
If X = 10 Or X = 11 Or X = 13 Then ElseIf X = 12 Or (X > 14 And X < 18) Then End If 'where as case.. Select Case X Case 10, 11, 13 Case 12, 15 To 17 End Select
Think about those who will follow you in coding (or maybe yourself in a few months). It would be easier to figure out what a Select Case statement does than a complex If...then...else statement.
It just plain reads BETTER!!!!
Don't Be a Will Allen!!!!!!!!
Not really...Quote:
Originally Posted by arpan_de
Cannot be replaced byCode:Select Case lngValue
Case 1
...
Case 2
...
Case 3
...
Case 4
...
End Select
Without using GOTO you cannot mimic the SELECT CASE behaviour of "jumping" to the END SELECT with IF/Blocks easily.Code:If lngValue = 1 Then
...
End If
If lngValue = 2 Then
...
End If
If lngValue = 3 Then
...
End If
If lngValue = 4 Then
...
End If
I could not agree more - maintainability of code is so much more important at times then simple old efficiency.Quote:
Originally Posted by randem
When we have a complex IF statement we sometimes code it like this:
This might seem like odd code but the alternative is:VB Code:
booDoSomething = True If booDoSomething Then If {check some condition to see if we really want to do this} Then booDoSomething = False End of End If If booDoSomething Then If {check some other condition (2) to see if we really want to do this} Then booDoSomething = False End of End If If booDoSomething Then If {check some other condition (3) to see if we really want to do this} Then booDoSomething = False End of End If If booDoSomething Then Call SomeFunction End If
This alternative might appear easier to write - but visit it 6 months later and add a NOT condition or an AND/OR condition and watch the bugs fly!VB Code:
If {check some condition to see if we really want to do this} _ or {check some other condition (2) to see if we really want to do this} _ or {check some other condition (3) to see if we really want to do this} Then Call SomeFunction End If
Quote:
The same happens in If...Else statements as well; as soon as the condition is satisfied, VB runtime will jump to the End If line.......
szlamany, what I meant was for a condition like this:Quote:
Not really...
& not for the If code snippet you have cited in post #7. SoVB Code:
If lngValue = 1 Then ... ElseIf lngValue = 2 Then ... ElseIf lngValue = 3 Then ... ElseIf lngValue = 4 Then ... End Ifcan very well be replaced by the If...Else conditional statements I have shown above. The Select...Case equivalent of the If conditions you have cited in post #7 would beVB Code:
Select Case lngValue Case 1 ... Case 2 ... Case 3 ... Case 4 ... End SelectVB Code:
Select Case lngValue Case 1 ... End Select Select Case lngValue Case 2 ... End Select Select Case lngValue Case 3 ... End Select Select Case lngValue Case 4 ... End SelectStatic, you have really shown a very good example to highlight the difference in readability between If....Else & Select...Case statements especially when the conditions turn out to be pretty complex.....great stufffffffff :)Quote:
Select case is more efficient.. as stated... and easier to use in some cases
Like:
I understand what you meant now...
I personally could never use the ELSEIF statement - what I appreciate about IF/THEN/ELSE blocks are the indented nature of them.
That ELSEIF statement never made any sense to me - makes for some hard to follow logic-flow.