|
-
Apr 28th, 2006, 03:26 PM
#1
Thread Starter
Frenzied Member
-
Apr 28th, 2006, 03:30 PM
#2
Re: Select...Case & If...Else!
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...
-
Apr 28th, 2006, 03:47 PM
#3
Thread Starter
Frenzied Member
Re: Select...Case & If...Else!
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.
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.......
ARPAN
IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!
NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!
PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?
-
Apr 28th, 2006, 03:48 PM
#4
New Member
Re: Select...Case & If...Else!
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.
-
Apr 28th, 2006, 03:49 PM
#5
Re: Select...Case & If...Else!
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
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Apr 28th, 2006, 04:01 PM
#6
Re: Select...Case & If...Else!
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!!!!!!!!
Last edited by randem; Apr 28th, 2006 at 04:43 PM.
-
Apr 28th, 2006, 04:03 PM
#7
Re: Select...Case & If...Else!
 Originally Posted by arpan_de
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.......
Not really...
Code:
Select Case lngValue
Case 1
...
Case 2
...
Case 3
...
Case 4
...
End Select
Cannot be replaced by
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
Without using GOTO you cannot mimic the SELECT CASE behaviour of "jumping" to the END SELECT with IF/Blocks easily.
-
Apr 28th, 2006, 04:40 PM
#8
Re: Select...Case & If...Else!
 Originally Posted by randem
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.
I could not agree more - maintainability of code is so much more important at times then simple old efficiency.
When we have a complex IF statement we sometimes code it like this:
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 might seem like odd code but the alternative is:
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
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!
-
Apr 28th, 2006, 05:34 PM
#9
Thread Starter
Frenzied Member
Re: Select...Case & If...Else!
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:
VB Code:
If lngValue = 1 Then
...
ElseIf lngValue = 2 Then
...
ElseIf lngValue = 3 Then
...
ElseIf lngValue = 4 Then
...
End If
& not for the If code snippet you have cited in post #7. So
VB Code:
Select Case lngValue
Case 1
...
Case 2
...
Case 3
...
Case 4
...
End Select
can 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 be
VB 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 Select
Select case is more efficient.. as stated... and easier to use in some cases
Like:
Static, 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
ARPAN
IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!
NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!
PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?
-
Apr 28th, 2006, 05:45 PM
#10
Re: Select...Case & If...Else!
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.
Last edited by szlamany; Apr 28th, 2006 at 07:09 PM.
Reason: oops - typo...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|