Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between switch statements in C# and C++

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?

like image 952
Abraham Avatar asked Nov 05 '12 20:11

Abraham


People also ask

What is the difference between switch case and nested IF condition?

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.

Is switch case faster than if?

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 .

Which is better switch or if-else?

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.

What is switch statement in C with example?

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.


4 Answers

C# has goto casevalue, which has all the benefits of fallthrough but is harder to do by accident.

Example on MSDN

like image 55
Ben Voigt Avatar answered Oct 18 '22 23:10

Ben Voigt


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.

like image 25
Sergey Kalinichenko Avatar answered Oct 18 '22 23:10

Sergey Kalinichenko


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.

like image 4
Matt Burland Avatar answered Oct 19 '22 00:10

Matt Burland


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).

like image 1
Cubic Avatar answered Oct 18 '22 23:10

Cubic