+3  A: 

Easier to read maybe...especially when you have lots of branches.

An article suggests it may be quicker....

http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx

Jobo
Some compilers can optimize switch statements into jump tables, making them faster.
Simucal
A jump table is quite slow. It can only faster if a lot of cases are present, and the frequency of the cases is similar.
Blaisorblade
+5  A: 

Switch can be optimized by compiler - you will get faster code.
Also I find it to be more elegant when dealing with enumerable types.

To sum up switch statement gives you performance + code elegance :)

Here are some useful links:

aku
Which compiler is this ?
Jobo
I'm assuming .net world since he is making links to C#
Simucal
for example C++ compiler does some interseting optimization
aku
The .sln file in the code example says "# Visual C# Express 2008"
Kevin Haines
A: 

One point is to make it easier to read. Instead of a slew of if-then-else keywords for each condition, you just use something like "case whatever:".

I don't know if compilers can optimize switch statements better than if-than-else statements, or not.

EndangeredMassa
+2  A: 

For expressiveness, the switch/case statement allows you to group multiple cases together, for example:

case 1,2,3: do(this); break;
case 4,5,6: do(that); break;

For performance, compilers can sometimes optimize switch statements into jump tables.

jdigital
A: 

Switch can be optimized "Better" by some compilers. There are pitfalls with using the switch statement in certain languages. In Java, the switch cannot handle strings and in VB2005 the switch statement will not work with radio buttons.
Switch can be faster and easier to read, If-Then is more generic and will work in more places.

WolfmanDragon
+1  A: 

Just to add another dimension. There is an argument to replace these conditionals with polymorphism.

http://googletesting.blogspot.com/2008/12/by-miko-hevery-google-tech-talks.html

BobbyShaftoe
+2  A: 

A very similar question was asked here.

Perpetualcoder
thanks for that link :)
Casey
A: 

Besides the other mentioned Code readability and optimisation in .NET you also get the ability to switch on enums etc

enum Color { Red, Green, Blue }; 

Color c = Color.Red;

switch (c) // Switch on the enum

{

// no casting and no need to understand what int value it is

case Color.Red:    break;
case Color.Green:  break;
case Color.Blue:   break;

}
Justin King
+8  A: 

A switch construct is more easily translated into a jump (or branch) table. This can make switch statements much more efficient than if-else when the case labels are close together. The idea is to place a bunch of jump instructions sequentially in memory and then add the value to the program counter. This replaces a sequence of comparison instructions with an add operation.

Below are some extremely simplified psuedo-assembly examples. First, the if-else version:

    // C version
    if (1 == value)
        function1();
    else if (2 == value)
        function2();
    else if (3 == value)
        function3();

    // assembly version
    compare value, 1
    jump if zero label1
    compare value, 2
    jump if zero label2
    compare value, 1
    jump if zero label3
label1:
    call function1
label2:
    call function2
label3:
    call function3

Next is the switch version:

    // C version
    switch (value) {
    case 1: function1(); break;
    case 2: function2(); break;
    case 3: function3(); break;
    }

    // assembly version
    add program_counter, value
    call function1
    call function2
    call function3

You can see that the resulting assembly code is much more compact. Note that the value would need to be transformed in some way to handle other values than 1, 2 and 3. However, this should illustrate the concept.

Judge Maygarden
The assembly code you generate is valid only if you can make the cases of the same size. Actually, nop padding and multiplying value before adding could do something like that.But instead, normally jump tables are used, to get something "goto *table.124[value]" after a range check.
Blaisorblade
And in many cases, the indirect jump is much slower than a single since branch prediction for indirect branches is often less effective.
Blaisorblade
Fair enough, I wanted to keep it simple. Also, I believe the C switch statement predates branch-prediction, and he wanted to know why it exists...
Judge Maygarden
+1  A: 

The ability to fall through several cases (intentionally leaving out the break statement) can be useful, and as a few people have already said it's faster as well. Perhaps the most important and least important consideration though, is that it just makes for prettier code than if/else. :)

Marc Charbonneau
+2  A: 

I'm ignoring this type of low level optimization as usually unimportant, and probably different from compiler to compiler.

I'd say the main difference is readability. if/else is very flexible, but when you see a switch you know right away that all of the tests are against the same expression.

Darron