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!
views:
323answers:
9So 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;
}
Every "if" statement has to be evaluated at runtime. This could depend on compiler optimisations.
Switch statements are more efficient in terms of evaluating the expression for each case as I understand it.
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
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.
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.
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.
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.
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
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.
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.