switch statement

Discussion in 'C++' started by bob@coolgroups.com, Dec 16, 2007.

  1. Guest

    I was just wondering if the switch statement usually generates more

    efficient code than an if/else. If not, then what is the main
    benefit

    of the switch statement?
    , Dec 16, 2007
    #1
    1. Advertising

  2. On Dec 16, 7:28 am, wrote:
    > I was just wondering if the switch statement usually generates more
    >
    > efficient code than an if/else. If not, then what is the main
    > benefit
    >
    > of the switch statement?


    As Stroustrup has written in section 6.3.2 of 3rd the switch statement
    is equivalent to if/else statement except that:
    1. Sometimes it is more readable that equivalent if/else, because
    testing a value against some constants are explicit.
    2. It can also lead to a better code generation. I think it is
    implementation dependent.

    The C++ Standard Document doesn't mention switch statements are more
    efficient than if/else statement.
    I think you use both statements from programming styles point of view.

    Regards,
    S. Amrollahi
    Saeed Amrollahi, Dec 16, 2007
    #2
    1. Advertising

  3. Ian Collins Guest

    wrote:
    > I was just wondering if the switch statement usually generates more
    > efficient code than an if/else.


    Measure!

    > If not, then what is the main
    > benefit of the switch statement?


    Legibility.

    --
    Ian Collins.
    Ian Collins, Dec 16, 2007
    #3
  4. Rolf Magnus Guest

    wrote:

    > I was just wondering if the switch statement usually generates more
    >
    > efficient code than an if/else.


    In some cases it does, in others it doesn't.

    > If not, then what is the main benefit
    >
    > of the switch statement?


    Better readability.
    Rolf Magnus, Dec 16, 2007
    #4
    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:
    1
    Views:
    422
    Enrique
    Feb 19, 2004
  2. vic

    switch case statement

    vic, Mar 3, 2004, in forum: Java
    Replies:
    9
    Views:
    5,822
  3. Replies:
    21
    Views:
    1,054
    Giannis Papadopoulos
    Aug 2, 2005
  4. bthumber
    Replies:
    5
    Views:
    420
    Alexey Smirnov
    Jan 29, 2009
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    106
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page