tags:

views:

323

answers:

9

I know what the basic idea there is, but I was thinking, and I realized, that it would therefore take MORE characters for a switch statement than for a bunch of if statements, since an if statement has 7 characters (not including the variable name, nor what it is being compared to, nor the code), while the switch statement has 9 characters (same thing), because of the word "break", and even without it, the word "case:" is 5 characters, compared to 7 from the if statement, so it isn't that much better than an if statement!

+15  A: 

So that you can do things like

switch (toupper(ch))
   {
   case 'A':
   case 'E':
   case 'I':
   case 'O':
   case 'U':
      bVowel = true;
      break;

   default:
      bVowel = false;
      break;
   }
franji1
+1: Very simple and practical example
Chubsdad
`case 'Y': bVowel = perhaps; break;`
James McNellis
LOL! how about `bVowel = (bool)0.125; // fuzzy logic bool`
franji1
Is bevoweling the opposite of disemvoweling?
Potatoswatter
A: 
  1. Every "if" statement has to be evaluated at runtime. This could depend on compiler optimisations.

  2. Switch statements are more efficient in terms of evaluating the expression for each case as I understand it.

  3. If you don't add the break the switch statement will continue down and could execute further cases. eg the default statement as well. eg if/else

  4. I prefer if/else if /else because they are harder to muck up if you forget the break. They take up less room, and they are more common with other coders. But you can make a switch statement look pretty.

John Ballinger
Every switch statement member is also evaluated at runtime. What do you think is going on, magic?
Billy ONeal
not if it is 1==1The compiler will default this to true. Thanks for being so polite.
John Ballinger
@billy, also see Ken Blooms statement below about how a compiler can build hash tables to quickly jump to cases quickly.
John Ballinger
@John: I'm not going to sugarcoat things when I see that they are blatantly wrong; sorry if my bluntness sounds rude, but it is what it is. My point is that yes, there are some cases where a compiler can speed lookups in a switch. That said, this is not always the case -- there's no magic thing the compiler does that makes the branches go away. Also, unless you have several hundred branches generating code that operates just like the IF/ELSE is going to be faster anyway.
Billy ONeal
I love it how this question has now been closed due to "Subjective" and "Argumentative"
John Ballinger
+1  A: 

You have a very odd way of deciding which is "better". In general "easier to think about" is a much more useful test than "shorter".

Ultimately, this is what the designers of the C language decided was the way that provided the best mix of readability and ease of compilation.

With any decent optimizing compiler, there's not likely to be much difference in the final generated program.

Ben Voigt
+3  A: 

I think it's a misfeature since the ability to fall through to the next block is confusing and error-prone. The common case of wanting to match multiple options would be better written:

switch (expression) {
   case 1, 7, 9:
      do something;
   case 2, 8, 10:
      do something else;
}

This wasn't an option for C++ though, since the language was designed to be relatively backwards compatible with C.

Paul Hankin
That syntax is nicer in that case, but there are still some things that you can do with switch the way it is that you can't do like that. For example, suppose case 3 does C, case 2 does B and C, and case 1 does A, B, and C. This is elegant with fall-through, but impossible with the syntax you propose (without repeating B once and C twice). I suppose the bottom line might be flexibility -- allowing fall-through is the maximally flexible way to provide switch (although whether fall-through should be implicit or explicit is another question).
Tyler McHenry
Agree with you,+1, but probably need a keyword to fall through when needed as @Tyler points out.
ergosys
A: 

for a switch statement, a compiler could, for instance, make a map of case values and goto addresses and do an optimised lookup, as opposed to just going through all the 'if' checks.

matt
+3  A: 

There are many important factors other than character count:

  • switch statements evaluate the expression once, whereas lazy coding of an if statement may request repeated evaluation... undesirable if expensive or having side effects
  • whether cases should fall through or need breaks varies per usage: you can save a lot of characters if the same action applies to many cases
  • optimisers should make switch statements at least as efficient as an if/else if/else chain IN EVERY USAGE, and for many usages it can be far more efficient (e.g. lookup value in an array to switch to the desired behaviour/result, build a binary search)
  • switch statements better document the single value deciding the flow, whereas if/else-if/else require the programmer to check each expression separately
Tony
A: 

It's so that things like Duff's Device can work.

Eclipse
I think that was more of an accident than anything else. And I think your getting a sense of dark amusement pointing a clear newbie at a very bad programming practice. :-)
Omnifarious
Well, yes and no. It does indicate how switch and case statements in C and C++ are closely related to goto statements and label. When you think of `switch()` as a sort of calculated goto, and `case ` as a type of label, then the reasons for `break` become apparent.
Eclipse
I had thought that the switch statement was for being lazier when you have a whole lot of comparisons to make
noah
+1  A: 

A compiler can come up with various different strategies for generating machine code for a switch statement, namely:

  • A lookup table, when the values tested for are very close together
  • Binary search, when there are a lot of values
  • Code that looks just like an if-then.

These days, the compiler may be smart enough to make such optimizations on a bunch of if statements, but that surely wasn't the case when the language was designed, and the switch statement puts all of the information right there for the compiler to look at and decide what code to generate.

Ken Bloom
A: 

C was designed to be easy to compile. Many aspects of the language which were pretty terrible, such as the old style parameter passing, have been successfully deprecated. Case statements were designed so that case labels didn't have to generate any code, but could be handled similarly to "goto" labels. Nowadays there's no particular need for the language to be easy to compile, but it would be impossible to change the way 'switch' statements work without breaking a lot of code.

supercat