-2

Possible Duplicate:
Advantage of switch over if-else statement
Why the switch statement and not if-else?

The switch statement seems to be totally useless. Anything it can do can be done by if and else if link.

They probably even compile to the same code.

So why bother having it?

The break statements in switch drives me crazy and that label: format reminds me of goto.

This is for objective-c, c, C++. I am not sure if vb.net has switch statement, but even if it does I must have forgotten because I never use it.

Community
  • 1
  • 1
user4234
  • 1,523
  • 1
  • 20
  • 37

4 Answers4

10

They may well compile to the same code. But the intent is not necessarily to provide better compiled code so much as it is to provide better source code.

You can do while or for loops with if and goto as well but that doesn't make while and for useless. Would you rather have:

for (i = 0; i < 10; i++)
    doSomethingWith (i);

or:

    i = 0;
loop12:
    if (! (i < 10))
        goto skip12
    doSomethingWith (i);
    i++;
    goto loop12
skip12:
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 4
    +1 for "You can do while loops with if and goto as well but that doesn't make while useless." – Yavar Jan 02 '13 at 06:39
3
if (color == WHITE)
{
}
else if (color == BLACK)
{
}
else if (color == GREY)
{
}
else if ((color == ORANGE) || (color == GREEN) || (color == BLUE))
{
}
else
{
}

vs

switch(color)
{
   case WHITE:
       break;
   case BLACK:
       break;
   case GREY:
       break;
   case ORANGE:
   case GREEN:
   case BLUE:
       break;
   default:
       break;
}

Isn't the latter more readable and requires lesser key strokes?


Apart from readability there's another unique use of switch-case: Duff's Device. This technique exploits the goto-ness of switch-case coupled with while.

void dsend(char* to, char* from, count) {
  int n = (count + 7) / 8;
  switch (count % 8) {
  case 0: do {
      *to = *from++;
      case 7: *to = *from++;
      case 6: *to = *from++;
      case 5: *to = *from++;
      case 4: *to = *from++;
      case 3: *to = *from++;
      case 2: *to = *from++;
      case 1: *to = *from++;
    } while (--n > 0);
  }
}
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • no the former is more readable and gives developer more freedom to make changes later for specific cases – Mahammad Isgandarli Mar 28 '20 at 09:36
  • I agree that readability is subjective, but I don't understand the freedom part. Can you please explain how `if` gives more freedom over `switch-case`? – legends2k Dec 07 '20 at 05:25
  • If you wanted to change the logic in `else if ((color == ORANGE) || (color == GREEN) || (color == BLUE))` for example, it would be easier using the else if. – whackamadoodle3000 Feb 09 '21 at 04:06
1

Performance of switch is same as if and else if blocks in the worst case. It may be better. This has been discussed before: Advantage of switch over if-else statement

Community
  • 1
  • 1
russoue
  • 5,180
  • 5
  • 27
  • 29
0

Pros:

Switch offers a better way to write program than if

Switch works faster than if because during execution compiler generates jump table to decide which case is satisfied rather than checking which case is satisfied!

Cons:

case can only have int or char constants or an expression that evaluates to one of these!

Anirudha
  • 32,393
  • 7
  • 68
  • 89