Valid, but worthless syntax in switch-case?

Through a little typo, I accidentally found this construct: int main(void) { char foo = ‘c’; switch(foo) { printf(“Cant Touch This\n”); // This line is Unreachable case ‘a’: printf(“A\n”); break; case ‘b’: printf(“B\n”); break; case ‘c’: printf(“C\n”); break; case ‘d’: printf(“D\n”); break; } return 0; } It seems that the printf at the top of … Read more

Switch case with fallthrough?

I am looking for the correct syntax of the switch statement with fallthrough cases in Bash (ideally case-insensitive). In PHP I would program it like: switch($c) { case 1: do_this(); break; case 2: case 3: do_what_you_are_supposed_to_do(); break; default: do_nothing(); } I want the same in Bash: case “$C” in “1”) do_this() ;; “2”) “3”) do_what_you_are_supposed_to_do() … Read more

Switch statement for string matching in JavaScript

How do I write a switch for the following conditional? If the url contains “foo”, then settings.base_url is “bar”. The following is achieving the effect required but I’ve a feeling this would be more manageable in a switch: var doc_location = document.location.href; var url_strip = new RegExp(“http:\/\/.*\/”); var base_url = url_strip.exec(doc_location) var base_url_string = base_url[0]; … Read more

C# switch on type [duplicate]

This question already has answers here: Closed 9 years ago. Possible Duplicate: C# – Is there a better alternative than this to ‘switch on type’? C# doesn’t support switching on the type of an object. What is the best pattern of simulating this: switch (typeof(MyObj)) case Type1: case Type2: case Type3: 5 Answers 5

Is returning out of a switch statement considered a better practice than using break? [closed]

Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 months ago. The community reviewed whether to reopen this question 5 months ago and left it closed: Opinion-based Update the question … Read more

Is there any significant difference between using if/else and switch-case in C#?

What is the benefit/downside to using a switch statement vs. an if/else in C#. I can’t imagine there being that big of a difference, other than maybe the look of your code. Is there any reason why the resulting IL or associated runtime performance would be radically different? Related: What is quicker, switch on string … Read more