I'm just starting out teaching myself C#, and in a tutorial on Switch statements, I read:
The behavior where the flow of execution is forbidden from flowing from one case block to the next is one area in which C# differs from C++. In C++ the processing of case statements is allowed to run from one to another.
Why does it stop after one case statement in C#? If you can use the break
statement to stop at any point, is there any reason in C# vs. C++ to having it stop after a match is found? And if you wanted more than one case in C#, would you have to use another Switch statement?
Check the Testing Expression: An if-then-else statement can test expressions based on ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or String object.
As it turns out, the switch statement is faster in most cases when compared to if-else , but significantly faster only when the number of conditions is large. The primary difference in performance between the two is that the incremental cost of an additional condition is larger for if-else than it is for switch .
Switch is generally faster than a long list of ifs because the compiler can generate a jump table. The longer the list, the better a switch statement is over a series of if statements.
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable.
C# has goto case
value, which has all the benefits of fallthrough but is harder to do by accident.
Example on MSDN
Technically, this is not correct: C# does allow fall-through when the body of the case
is empty:
switch(val) {
case 1:
case 2:
Console.WriteLine("small");
break;
case 3:
case 4:
case 5:
case 6:
case 7:
Console.WriteLine("medium");
break;
default:
Console.WriteLine("large");
break;
}
Allowing implicit fall-through after a non-empty body in C/C++ is done by mistake more often than not. That is why the designers of C# decided against allowing it.
I think the argument is that fall through switch statements in C++ generally caused more problems than they solved. I.e. when they fell through when it wasn't the programmers intent, they just forgot the break
. So C# did away with it.
Same with a lot of other "features" in C++. It's not that they weren't occasionally useful, it's just that more often they were harmful. Like evaluating just about anything as a bool, so in C# you can't do:
if(1)
{
}
Because evaluating ints as bools caused a lot of hard to find bugs.
C#'s version is less error prone - nothing will explode in your face if you forget to write a break
, which happens. It also looks a little nicer. Then again, there's precious little reason to use switch statements most of the time anyways (often it just ends up being a poor implementation of type dispatch, which is built-in in both languages through class inheritance).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With