Thread: Switch Case vs if else

  1. #1
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102

    Switch Case vs if else

    Does a switch case any sort of optimization(in VC++ 6) to make it faster than if else, or is it just easier to code?
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    If there were difference in speed, it would be so small it wouldn't even matter.
    *Cela*

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Case had a disatvantage over if but i forget it at the moment...

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    Re: Switch Case vs if else

    Originally posted by WebSnozz
    Does a switch case any sort of optimization(in VC++ 6) to make it faster than if else, or is it just easier to code?
    IF can be used with variable types that switch cannot. As far as speed difference, as said before it'd be so small you'd never notice it. Sometimes a program needs a switch, sometimes it needs an if, sometimes it needs both. Its all about implementation.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    I can't imagine a program NEEDING case or if?
    Can you give an example?

  6. #6
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    well you can always use macros, i'm talking like obvious things such as:


    if (variable == something)
    {
    ...
    }

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    I don't like case anyway because it looks like basic

  8. #8
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I perfer a switch in cases such as character input.

    We had this debate in class today, ironically. Some kid said, stupidly, "Well why can't i just write my program with a bunch of if statements?" The assignment consisted of about 25 checks on user input, 50% of which were character, and imo to use all ifs or a mojority of ifs wouldn't be efficent.

  9. #9
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    I thought that since the switch case doesn't work with variables, then it's not checking each and every variable to see does (switchVariable == case), but instead is using maybe some sort of boolean search, where all the case values are organized into a red black tree at compile time. If this, or something similar, were the case, then performance would be greatly improved when you have a large amount of case statements versus the same number of if else statements. If it does use a red black tree search, case would be O(log2 n) versus O(n) for if else. Which will go from a small difference to a huge difference in speed if you get more and more values you need to check.

    Also any small amount of improved performance is multiplied many times whenever it is in a frequently executed part of the code.

    It's also possible that if else statements and switch case compile into very similar machine code. But it would seem if that were true, then variables could be used as the key value in the case statement.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  10. #10
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I thought that since the switch case doesn't work with variables
    Well than what do u suppose it works with? :P

    Code:
    char im_a_variable;
    
    some cout for a letter
    
    switch(im_a_variable)
    {
    ...
    }

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Well than what do u suppose it works with?
    Agreed. (However, not with the spelling. )

    Switch statements work with integral values only. Doesn't mean they're not "variables".

    Let's be careful of the 'basics'.

    It's incumbent upon the programmer to know when a series of IF statements are preferred to a SWITCH statement.

    Execution speed, in this particular instance, should play a secondary role to "readability". Frequently repeated code should be functionalized. Ergo, we're back to "readability".

    My opinion...

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  12. #12
    Speedy5
    Guest
    Glad you asked! I was once curious about this too, and I made my own speed test:

    Testing...
    If: 4806
    Switch: 2464
    Op. If: 761
    <cut off from other tests>

    It really depends on the way you program that block of code. Each test is done 50000000 times on a P2 366Mhz laptop with music running, lol. The times are in milliseconds.


    This is the If:

    if (n==0)
    n++;
    else if (n==1)
    n++;
    else if (n==2)
    n++;
    ...
    else if (n==18)
    n++;
    else
    n=0;


    This is the switch:
    switch (n)
    {
    case 0:
    n++;
    break;
    case 1:
    n++;
    break;
    ...
    case 18:
    n++;
    break;
    default:
    n=0;
    }




    This is the optimized if:
    if ((n++)==19)
    n=0;

  13. #13
    Speedy5
    Guest
    BTW, I ran the test various times and the same results appeared. This was done on MS C++.NET (unmanaged code (normal, native)).

  14. #14
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    An Added Information

    1 S) Switch case statements work only with integral types
    1 F) If statements work with all data types

    2 S) Every case statement must be followed by a constant
    2 F) If statements can work with constants / variables / expressions

    3 S)Switch case allows for equality check only (==) w.r.t switch expression and the case constant
    3 F) If statements support all relational / logical operators
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  15. #15
    Registered User
    Join Date
    Jan 2003
    Posts
    37
    The switch statement evaluates an expression that yields an integer value and branches to one of a number of labelled sections of code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM