Select Case vs If..ElseIf..

  1. 4 months ago

    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
  2. Edited 4 months ago

    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

  3. Jonathan E

    Aug 8 Pre-Release Testers, Xojo Pro Las Vegas, NV

    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).

  4. Norman P

    Aug 8 Pre-Release Testers, Xojo Pro under THE bus

    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

  5. Russ L

    Aug 8 Pre-Release Testers, Xojo Pro

    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?

  6. Dave S

    Aug 8 San Diego, California USA

    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
  7. Dave S

    Aug 8 San Diego, California USA

    @SylvainGuillemette while being easier to read, is significantly slower..

    .08ms is "significant"? (ie. per execution)

  8. @Dave S .08ms is "significant"? (ie. per execution)

    For my application, yes.

  9. Richard D

    Aug 9 Pre-Release Testers, Xojo Pro Europe (UK, London)

    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

  10. Jeff T

    Aug 9 Pre-Release Testers Midlands of England, Europe

    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
    
  11. Emile S

    Aug 9 Europe (France, Strasbourg)

    @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.

  12. Greg O

    Aug 9 Xojo Inc scout.galaxy.barn

    @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.

  13. Richard D

    Aug 9 Pre-Release Testers, Xojo Pro Europe (UK, London)

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

or Sign Up to reply!