Is there an advantage using one over the other?
Mostly readability maybe?
Select Case i Case 1 Case 2 Else End Select If i = 1 Then ElseIf i = 2 Then Else End If
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
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
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
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
@Jeff T So it makes sense to put the most likely cases at the beginning of the cases
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.
@Richard D 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
In case you’ve never done this, you can use arbitrary cases by using this form:
Select Case True Case a = 1 Case Foo = “bar” Case Left(Foo, 5) = “hello” Case tf = false End Select
Personally I find Select-Case more readable than if-then, so beyond one condition, I usually use it.