Switch() vs if else if

Discussion in 'C++' started by Gurikar, Apr 29, 2005.

  1. Gurikar

    Gurikar Guest

    Whats the difference b/w swicth case and if else if.
    Iam finding both are same when you are using in application. Only
    difference if else if more flexible to use with all data types. But
    some people says switch case is faster than if else if, i dont know is
    it and why is it??

    Regards
    Gurikar, Apr 29, 2005
    #1
    1. Advertising

  2. Gurikar

    Sharad Kala Guest

    "Gurikar" <> wrote in message

    > Whats the difference b/w swicth case and if else if.
    > Iam finding both are same when you are using in application. Only
    > difference if else if more flexible to use with all data types. But
    > some people says switch case is faster than if else if, i dont know is
    > it and why is it??


    Well, you could read about jump table optimization. But measure and then
    only believe what you read.

    Sharad
    Sharad Kala, Apr 29, 2005
    #2
    1. Advertising

  3. Gurikar

    Rolf Magnus Guest

    Gurikar wrote:

    > Whats the difference b/w swicth case and if else if.


    The former can only used with integral types, and the case values need to be
    compile-time constants. An if/else cascade is likely to be slower than
    switch/case.

    > Iam finding both are same when you are using in application. Only
    > difference if else if more flexible to use with all data types. But
    > some people says switch case is faster than if else if, i dont know is
    > it and why is it??


    Heh, I wrote the above before I read this. Well, the reason is that
    switch/case is often implemented using a jump table with the case values as
    index into the table. The if/else is usually implemented using a cascade of
    conditional jumps.
    Rolf Magnus, Apr 29, 2005
    #3
  4. Gurikar wrote:

    > Whats the difference b/w swicth case and if else if.

    How about a jump table, too?

    The switch statement only handles integral quantities.
    Compilers may optimize the switch statement into a jump table
    (see below).

    An if-else-if ladder can handle any type, such as a string.
    This construct is more difficult for a compiler to optimize.

    A jump table is either a table of addresses (pointers) or
    jump instructions. An index is used to access the appropriate
    location, then an action is taken. This can be implemented
    in C++ using an std::map of <key, function_pointer> or an
    array of similar structures.


    > Iam finding both are same when you are using in application. Only
    > difference if else if more flexible to use with all data types. But
    > some people says switch case is faster than if else if, i dont know is
    > it and why is it??

    Only believe "faster than" when actual profiling has been
    performed. And only worry about "faster" when the program
    is too slow.


    >
    > Regards
    >

    The best construct to use is the one that is the easiest
    to understand to the reader.

    I prefer to use jump tables, because the data can change
    without having to retest the jump-table driver (engine).

    All these constructs depend on the situation.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
    Thomas Matthews, Apr 29, 2005
    #4
  5. Gurikar

    msalters Guest

    Gurikar wrote:
    > Whats the difference b/w swicth case and if else if.
    > Iam finding both are same when you are using in application. Only
    > difference if else if more flexible to use with all data types. But
    > some people says switch case is faster than if else if, i dont know

    is
    > it and why is it??
    >
    > Regards


    Besides the obvious jumptables, compilers can also generate nested
    if-else constructs instead of lineair if-elseif chains. In fact,
    these can be mixed.

    Asumme you have cases 1..10 and 101..110. Now, a jumptable might be
    inconvenient, but implementing such a switch internally as a single
    if(x<10) __goto jump[x] else if (x>100&&x<110) __goto jump[x-90]
    is certainly legal. Let the compiler deal with those details.

    If you would write such code, you'd have to review it every time you
    add an enumerator. So does a compiler, but it's a lot faster and
    makes less mistakes. Besides, if you did that your source becomes
    unreadable. If the compiler does this to your switch, only the
    assembly becomes unreadable.

    Regards,
    Michiel Salters
    msalters, Apr 29, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    25
    Views:
    660
    MonkeeSage
    Oct 1, 2006
  2. hasadh

    switch and if..else same at assemby code ?

    hasadh, Sep 7, 2004, in forum: C Programming
    Replies:
    11
    Views:
    554
    Bob Jenkins
    Sep 14, 2004
  3. kj
    Replies:
    15
    Views:
    550
    Lawrence D'Oliveiro
    May 23, 2009
  4. A
    Replies:
    8
    Views:
    798
    James Kanze
    Aug 28, 2010
  5. Gergely Kontra
    Replies:
    16
    Views:
    230
    Devin Mullins
    Sep 20, 2005
Loading...

Share This Page