Questions tagged [switch-statement]

In computer programming, a switch, case, select or inspect statement is a type of selection control mechanism used to invoke specific blocks of code based on variable contents.

In computer programming, a switch, case, select or inspect statement is a type of selection control mechanism that exists in most imperative programming languages such as Pascal, Ada, C, C++, C#, Java, and so on. It is also included in several other programming languages. Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multi-way branch (or "goto", one of several labels). The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases.

Example pseudo-code:

switch (response)
   when "Y"
      // code to execute when variable response="Y"
   when "N"
      // code to execute when variable response="N"          
   else
      // code to execute if no when clauses are true
end switch

Be sure to also include a tag specifying the programming language your question relates to, such as or .

11704 questions
351
votes
21 answers

Should switch statements always contain a default clause?

In one of my first code reviews (a while back), I was told that it's good practice to include a default clause in all switch statements. I recently remembered this advice but can't remember what the justification was. It sounds fairly odd to me…
tttppp
  • 7,723
  • 8
  • 32
  • 38
340
votes
10 answers

Switch statement for greater-than/less-than

so I want to use a switch statement like this: switch (scrollLeft) { case (<1000): //do stuff break; case (>1000 && <2000): //do stuff break; } Now I know that either of those statements (<1000) or (>1000 && <2000) won't work (for…
switz
  • 24,384
  • 25
  • 76
  • 101
335
votes
4 answers

Error: Jump to case label in switch statement

I wrote a program which involves use of switch statements, however on compilation it shows: Error: Jump to case label. Why does it do that? #include int main() { int choice; std::cin >> choice; switch(choice) { …
Frustrated Coder
  • 4,417
  • 10
  • 31
  • 34
323
votes
7 answers

Test for multiple cases in a switch, like an OR (||)

How would you use a switch case when you need to test for a or b in the same case? switch (pageid) { case "listing-page": case "home-page": alert("hello"); break; case "details-page": alert("goodbye"); break; }
Andres
  • 5,002
  • 6
  • 31
  • 34
317
votes
23 answers

Why can't the switch statement be applied to strings?

Compiling the following code gives the error message: type illegal. int main() { // Compilation error - switch expression of type illegal switch(std::string("raj")) { case"sda": } } You cannot use string in either switch or…
yesraaj
  • 46,370
  • 69
  • 194
  • 251
286
votes
5 answers

Why does Java switch on contiguous ints appear to run faster with added cases?

I am working on some Java code which needs to be highly optimized as it will run in hot functions that are invoked at many points in my main program logic. Part of this code involves multiplying double variables by 10 raised to arbitrary…
282
votes
12 answers

Is 'switch' faster than 'if'?

Is a switch statement actually faster than an if statement? I ran the code below on Visual Studio 2010's x64 C++ compiler with the /Ox flag: #include #include #include #define MAX_COUNT (1 << 29) size_t counter =…
user541686
  • 205,094
  • 128
  • 528
  • 886
281
votes
3 answers

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

Option 1 - switch using return: function myFunction(opt) { switch (opt) { case 1: return "One"; case 2: return "Two"; case 3: return "Three"; default: return ""; } } Option 2 - switch using break: function myFunction(opt)…
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
278
votes
4 answers

Swift: Test class type in switch statement

In Swift you can check the class type of an object using 'is'. How can I incorporate this into a 'switch' block? I think it's not possible, so I'm wondering what is the best way around this.
kingrolo
  • 2,857
  • 2
  • 16
  • 11
276
votes
15 answers

How to use null in switch

Integer i = ... switch (i) { case null: doSomething0(); break; } In the code above I can't use null in the switch case statement. How can I do this differently? I can't use default because then I want to do something…
hudi
  • 15,555
  • 47
  • 142
  • 246
269
votes
21 answers

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…
Matthew M. Osborn
  • 4,673
  • 4
  • 25
  • 26
262
votes
12 answers

How to use a switch case 'or' in PHP

Is there a way of using an 'OR' operator or equivalent in a PHP switch? For example, something like this: switch ($value) { case 1 || 2: echo 'the value is either 1 or 2'; break; }
alex
252
votes
5 answers

C# switch on type

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: …
Adam
  • 2,523
  • 2
  • 15
  • 4
227
votes
10 answers

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 =…
Dr. Frankenstein
  • 4,634
  • 7
  • 33
  • 48
223
votes
5 answers

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: …
Mischka
  • 2,239
  • 2
  • 14
  • 3