Select Case vs If..ElseIf..

Is there an advantage using one over the other?
Mostly readability maybe?
Thank you!

Select Case i
Case 1
  
Case 2
  
Else
  
End Select


If i = 1 Then
  
ElseIf i = 2 Then
  
Else
  
End If

So I just benchmarked both, and Select Case, while being easier to read, is significantly slower…

Loop: 500K

Select Case
i = 1 52300 ms
i = 2 63600 ms
i = 3 77600 ms

If… ElseIf
i = 1 37500 ms
i = 2 37600 ms
i = 3 37800 ms

Thanks

I have often wondered this myself and have yet to do any extensive benchmarking, but I seem to see performance even out when using case statements to evaluate an expression. I assume that if would evaluate each expression as it steps over each option where case would evaluate it only once and compare answers, but I could be wrong here. Perhaps Xojo could chime in since they know how it works under the hood (or someone who has already gotten the answer to this question).

there are at least 3 ways to do this

  1. if then else
  2. select case
  3. jump table of delegates

they all have slightly different best and worst cases depending on how you use them

write code that is clear clean and readable
then benchmark it so if this is actually a slow point in your code
and if so then explore which option might boost your speed
often the best boost is simply a different algorithm for whatever problem it is your trying to solve

trying to optimize at this level before writing your code is pointless as this may NEVER be a slow point in the code

I’d say case only really comes into its own if you have more than 2 choices or when you have two choices but expect the choices to expand.

Given the above, would you use if, or case?

If I KNOW for a fact there can be only two choices, and no way the app or user could inject a 3rd choice I would

if x=choice_1 then 
 ....
else
....
end if

if there was even a remote possiblity of an error from a 3rd value then

if x=choice1 then 
...
else if x=choice2 then 
....
else
msgbox "You messed up"
end if

.08ms is “significant”? (ie. per execution)

For my application, yes.

i usually use select case if there is a same X to check.
sometimes the code have a different type of condtion, then i use the if… then else…else…end if

I did wonder if ALL cases were evaluated in a select.
Which would mean that if there were 20 possible values, 20 tests were carried out.
That would certainly slow things down compared to a simple branch as per if…then

But Select stops evaluating when the first matching condition is found.
If the first match is the 20th, then 19 more tests have been carried out.
So it makes sense to put the most likely cases at the beginning of the cases

dim testvar as integer = 6

select case testvar
case 6
  msgbox "This one fired"
case 8
  Msgbox "not me"
case 5 to 7
  msgbox "well.. another one!"     --never fires
case 4,5,6
  msgbox "3 times now if it arrives here"     --never fires
case else
  
end select




select case testvar
case 4,5,6
  msgbox "Evaluated First"
case 6
  msgbox "This one fired too"    --never fires
case 8
  Msgbox "not me"
  
case else
  
end select

At the “readability” expense (lack of / error prone). But you are right.

As Norman said, this depends on the use.

If this “sort” is executed once, who care the wasted time (if any) ?

If in a loop, a design / code rewrite may have to be done.

[quote=449858:@Richard Duke]i usually use select case if there is a same X to check.
sometimes the code have a different type of condtion, then i use the if… then else…else…end if[/quote]
In case you’ve never done this, you can use arbitrary cases by using this form:

[code]Select Case True
Case a = 1

Case Foo = “bar”

Case Left(Foo, 5) = “hello”

Case tf = false

End Select[/code]

Personally I find Select-Case more readable than if-then, so beyond one condition, I usually use it.

@Greg O’Lone , i did not know we can do that. learn something new everyday