Results 1 to 20 of 20

Thread: Select Case vs If-Then-Else

  1. #1

    Thread Starter
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330
    Hi,
    Is there a rule-of-thumb as to when to use Select Case or If/Then/Else statements?
    What are the pros and cons to each?
    Thanks,
    Al.
    A computer is a tool, not a toy.

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    Rule of Thumb

    There is no Rule of Thumb but it is considered good parctice to use Select Case when you have a number of conditions. Using If, if you have several conditions createds code that is not easily read...

    If this then
    do that
    if that then
    do this
    if nothing then
    do something
    endif
    endif
    endif
    Add a few else if's and you have a hornets nest.

    Case on the other hand is very easily read.
    Case 1
    do this
    Case 2
    do that
    Case 3
    Got it
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    If anyone know if there's gonna be any expanding and collapsing of if/for/do/while/select in vb7 i would be glad to know...
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Guest
    Usually If...Then is an Either THIS or THIS statement. In other words, it's usually only for 2 outcomes.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Meg, no you can have elseif's between, so actually there isn't much differing it from select case (i havent tested performance yet)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374
    Select Case can also reduce the amount of code when you have a number of conditions, e.g. in an error handler
    Code:
    Select Case Err.Number
         Case 1
              Msgbox "Yadda Yadda 1"
         Case 2
              Msgbox "Yadda Yadda 2"
         Case 3
              Msgbox "Yadda Yadda 3"
         Case 4
              Msgbox "Yadda Yadda 4"
         Case 5
              Msgbox "Yadda Yadda 5"
         Case 6
              Msgbox "Yadda Yadda 6"
         Case Else
              Msgbox "Yadda Yadda Else"
    End Select
    versus
    Code:
    If Err.Number = 1 Then
         MsgBox "Yadda Yadda 1"
    Elseif Err.Number = 2 Then
         MsgBox "Yadda Yadda 2"
    Elseif Err.Number = 3 Then
         MsgBox "Yadda Yadda 3"
    Elseif Err.Number = 4 Then
         MsgBox "Yadda Yadda 4"
    Elseif Err.Number = 5 Then
         MsgBox "Yadda Yadda 5"
    Elseif Err.Number = 6 Then
         MsgBox "Yadda Yadda 6"
    Else
         MsgBox "Yadda Yadda Else"
    End if

  7. #7
    Guest
    not the final truth, but a hint in the (hopefuly right direction). as with all rules, there are plenty exeptions. I wouldn't bet, but i thing 'Select Case' is faster.

    if you have to check several conditions of one variable, 'Select Case' is probably more elegant:

    Select Case i
    Case 1, 6, 9, 27: do something
    Case 2, 11, 14: do something different
    Case Is > 30: that's a lot
    Case Else: everything else
    End Select

    if you need to check condiditions for more variables, 'If Then Else' could produce shorter code:

    If i >= 1 And j < 255 Then

    ElseIf Whatsoever

    End If

  8. #8
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    I just tested both and the ifs are definitely faster...

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yes, same results here
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    Not sure about VB but in C the case statement is faster but in C there is no "elseif" statement, just an else followed by another if. so all the expressions get evaluated unlike the case statement.

    VB has an elseif statement so ....?
    dunno !!

    I use case statements if I have more than two elseifs or if I think more will be added later. it's easier to read.

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  11. #11
    Guest

    Talking Paul and Meg are right...kedaman hmmmmm

    Ok you have an object which can have two colours...red or blue, therefore an IF statement

    If Object.color = Blue Then
    'whatever
    else
    ' colour is not blue therefore red
    End If

    Now if the object could be red, blue, yellow, green etc

    Select Case Object.colour
    case red
    'whatever
    case blue
    'whatever
    case yellow
    'whatever
    case green
    'whatever
    case Else ' could be Otherwise caren't remember
    'whatever
    End Select

    Use If...Then...Else for either/or conditions, Case for multiple conditions.

    The fact that you can use Else If doesn't equate an If statement to a Case statement kedaman:0

  12. #12
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Re: Paul and Meg are right...kedaman hmmmmm

    A fellow Aussie to the rescue eh?

    How was the free train ride the other day?
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  13. #13
    Guest

    Thumbs down Paul

    Didn't bother with it...another government stunt to appease the populous....work in North Ryde...no station anyway.

    Q. Whats the difference between a Ferret and John Howard?

    A. A ferrit can go down your pants, John Howard is only interested in your back pocket

  14. #14
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Talking

    He's a bit of looser.

    I read the SMH every day from Tokyo on the net to keep up to date.

    He got screwed on the Harbour Bridge walk


    How many John Howards does it take to change a light bulb?

    One to promise it
    one to argue whether the service needs GST
    One to debate it


    But it won't happen

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    a) How many posts that it take to change a lightbulb?
    b) How many lightbulbs does it take for a programmer to see anything?
    c) How many lightbulbs do you get with 20 finnish mark?
    d) How long does a normallifetime 40W 50hz last?
    e) How dark is it in Tokyo at now?
    f) How many lightbulbs do you need?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Cool

    a) 1
    b) 0 (Computer screen gives enough light)
    c) 40
    d) 750hrs
    e) Very, except for the neon
    f) 1 flourescent tube would be fine... email it to me plwase
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I don't know the Yens rate to mk but its probably between 1:20 to 1:30
    but 1$ is about 5mk or 6€
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  18. #18
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Talking

    I was going to look it up since I was online but I thought it'd be easier to take a guess, wrongly it seems.

    The yen is 'suffering' from EnDaka, meaning it's very expensive, 100y = US$1.07

    The good thing is, AUD$1.00 = 61Y because the Aussie PM is a moron so I get a fat bonus here which is worth say the price of 10 fridges, but when I send the money to Australia (Oz) it's worth about 25 fridges. And, considering the difference in cost of living between Tokyo and Sydney I can live on one bi-annual bonus for 4-5 months in Sydney!

    If I stay here for another couple of years before I head home and keep playing with my E*trade account I might be able to buy a house!!!!! (in sydney of course, not tokyo - where I struggle to afford a post office box )



    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  19. #19
    Guest

    Talking Paul....Paul....Paul

    The Aussie PM is a mental giant compared to Alexander Downer, Andy Pandy, the teletubbies, and Pauline Hanson........actually Hanson and Howard have about the same IQ, loitering around the low 40s

  20. #20
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    She got screwed again in NSW, de-registered!!!!!

    Serves the racist ***** right.

    Did she do the reconciliation walk over the bridge? not bloody likely.

    Poiliticians suck in every country though, you should see this place, when it comes to corruption, maybe the Aussie poli's being stupid is not so bad. Public money just vanishes here
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width