Results 1 to 24 of 24

Thread: The case for Case vs. If

  1. #1

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Could someone please explain to me the advantages of using Case instead of If? I feel I am missing out...

  2. #2
    Hyperactive Member
    Join Date
    Dec 1999
    Posts
    321

    Not much advantage

    It's just less code and simpler to follow, that's all.
    Signed, Rodik ([email protected])
    Programmer,usesVB6ED
    ===========================
    Copyright©RodikCo,2002.

    Dont mind this signature ;] Its old

  3. #3

    Thread Starter
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Thanks Rodik, an honest answer I feel...

  4. #4
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ....

    Actually, yes, it is a less code and less headache thing. But you need to know when to use what. Whenever you want to choose, if you have two things to choose from, use 'if'. If you have more than 3 things to choose from, use 'case'. If you have 3 things, (take one and pass on the remaining to me!) you can use either 'if' or 'case', whatever you like.

    The important point is 'two is a company, three is a crowd' applies to the 'if' statement. Whenever the number of choices increases, 'if' will become complicated to follow.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  5. #5
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Santo Domingo,D.N., Dom. Rep.
    Posts
    707
    In Fact VB take less time to translate a CASE than IF

  6. #6
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Cleveland, Ohio
    Posts
    263
    Just note that large case statement will be faster than an if statement. If you use multiple if statements, it has to check each one before continuing on to the next. If you use one large case statement, it automatically matches a case and goes right to it, doesn't have to check all the other cases.

  7. #7
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088

    Wink

    So what about ElseIf? I made some performance tests and the Ifs were always faster than Select Case...

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    How many case/elseif statements did you use?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Cleveland, Ohio
    Posts
    263
    All I know is I had a function once, it was all If, and then I changed it to a Case, and it went much faster. I would like you to perform some tests, and have the results posted on my desk by twenty hundred hours.

  10. #10
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367

    Paul - the aussie in Japan

    Maybe you could get Paul - the aussie in Japan do some performance tests, he seems sharp and knows the workings of getTickCount.


  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What d'ya mean - workings? It's dead simple!

    Code:
    lBefore = GetTickCount
    
    ...
    
    lElapsed = GetTickCount - lBefore
    lElapsed has the time taken for ... to execute (in ms).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    I didnt say it was difficult, I am just too dam lazy to do it, but I figure ya throw Paul a nice line, he might do it.

    I am interested in knowing which is faster, but have no drive to do it myself.

  13. #13
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    Yeah, and to work out which one's faster you could use a Case or an If statement.

    By the way, is IIf slower then an If...ElseIf statement?
    Courgettes.

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    IIf is definitely slower than If...ElseIf
    Also done tests and IF, elseif, elseif...End if is faster than select case.

    For performance testing, if you use an loop with for next you will get missleading results, and that's what you need to do with gettickcount. For more exact results you could use querryperformace counter, which is accurate to about 1 microsecond, also dependent on hardware...
    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.

  15. #15
    Fanatic Member
    Join Date
    Jan 1999
    Location
    UK
    Posts
    554
    Code:
    If InDoubt Then
      Select Case of InDoubt
        Case Something
          Dummy="Hmmmmmmmmmmmm"
        Case Else
          DoEvents()
      End Select
    Else
      DoEvents()
    End If
    Hmmmmmmmmmmmm,
    Makes one stop and think...
    DocZaf
    {;->


  16. #16
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    Hi!, Spotted my name and thought I'd drop in.

    I think we went through this a while ago and were all dissapointed to find out that heaps of elseif's were faster than a case statement, we were hoping to find that the case was faster because it looks cleaner.

    In C the case statement is losts faster because it can skip to the bottom of the statement after it executes the True code, but C doesn't have an elseif statement, just else if (a new if statement after the else) which means losts of extra evaluation.

    Personally I try never to use more than 1 or 2 elseifs in an if statement, but that's for readablilty, mostly the impact of using a case statement is so small that there are almost always better perfomance tunings that can be done.

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

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

  17. #17
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ....

    All talking about whether IF is faster or CASE is faster, where the time wasted (if any) in any method has to be measured in micro/mili/nano seconds (whichever is the smallest unit) ?????

    Let me ask you all a simple question: You have to use either IF or CASE in your project. You have to make a choice from two options, what would you choose? And if you were to choose one from 15 options, what would you choose?

    I feel you should also consider the debugging strain on the programmer. It should not be that you saved 16 miliseconds by using IF instead of CASE and lost 16 hours in debugging the nested IFs.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  18. #18
    Frenzied Member /\/\isanThr0p's Avatar
    Join Date
    Jul 2000
    Location
    They can't stop us! We're on a misson from God.
    Posts
    1,181

    Talking

    Hm 16 Millicsecond in the main loop would quite nice!
    Sanity is a full time job

    Puh das war harter Stoff!

  19. #19
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Told ya Paul would know...

    Thx Paul

  20. #20
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    The speed difference is minimal, as in unless you need to loop it over 100,000 time, you'll not notice. Use which ever makes you're code more readable and understandable.

    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

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

  21. #21
    Guest

    Exclamation C++

    Paul, regarding using if's in C++. You don't necessarily have to use else whenever you are using a lot of if's.

    This would work fine as well.

    Code:
    if( Choice == '*' )
        cout << "Multiplication";
    if( Choice == '/' )
        cout << "Division";
    if( Choice == '+' )
        cout << "Addition";
    But, yes, I agree wih you on using a switch statement instead because you easily insert a break into it.

  22. #22
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Although in many cases this wouldn't work because you need to avoid the possibility of satisfying more than one if statement.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  23. #23
    Guest

    Thumbs up Gee this is real interesting

    Paul is dead right, in any version of basic l have used vb/Universe/Pick etc etc. Continuos If statements are fractionally faster than Case statements, strange but true.

    Even though each one of the IF statements has to be tested, the CASE statement does it in a sneaker way too

    E.g
    Code:
       Select Case iValue
          Case 1
          Case 2
          Case 3
          Case 4
       End Select
    
    
       If iValue = 1
       If iValue = 2
       If iValue = 3
       If iValue = 4
    Now suppose iValue = 1, the first If condition is executed with each subsequent If being tested. With the Case statement, the first Case is executed followed by each line of code being tested for the End Select statement. Hence the apparent paradox nature of the speed test.

    Of course with the If construction you would throw in an Exit Sub or whatever to by-pass the redundant testing.

    Its up to you and your style of coding. Cause a number of people will end up modifying my code over time, l use case for large numbers of tests to allow for greater readability. Phantomd uses nested If statements, because he prefers them....don't trust a unix guy to do anything easy...

    Just sit back and think...gee in a couple of years l might have to modify this code, would l be able to read it easily to make a change.

  24. #24
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Santo Domingo,D.N., Dom. Rep.
    Posts
    707

    Case VS IF

    Originally posted by coox
    Could someone please explain to me the advantages of using Case instead of If? I feel I am missing out...
    Not only code is easest for read, Computer only test var once

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