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 .

10485 questions
281
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…
271
votes
6 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" || "home-page": alert("hello"); break; case "details-page": alert("goodbye"); break; }
Andres
  • 4,402
  • 6
  • 28
  • 33
269
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
  • 21,124
  • 20
  • 72
  • 100
268
votes
21 answers

Why the switch statement cannot be applied on strings?

Compiling the following code and got the error of 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 case.…
yesraaj
  • 42,284
  • 65
  • 185
  • 246
253
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
  • 189,354
  • 112
  • 476
  • 821
247
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
236
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…
Code Maverick
  • 19,231
  • 10
  • 57
  • 111
236
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,407
  • 2
  • 12
  • 10
233
votes
20 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,213
  • 4
  • 23
  • 25
230
votes
13 answers

How to use null in switch

Integer i = ... switch (i){ case null: doSomething0(); break; } In the code above I cant use null in switch case statement. How can I do this differently? I can't use default because then I want to do something else.
hudi
  • 13,245
  • 40
  • 125
  • 230
213
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,133
  • 2
  • 13
  • 4
209
votes
9 answers

Switch statement for string matching in JavaScript

How do I write a swtich 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,394
  • 7
  • 30
  • 48
208
votes
8 answers

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':…
abelenky
  • 58,532
  • 22
  • 99
  • 149
205
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,059
  • 2
  • 13
  • 3
193
votes
10 answers

Switch statement: must default be the last case?

Consider the following switch statement: switch( value ) { case 1: return 1; default: value++; // fall-through case 2: return value * 2; } This code compiles, but is it valid (= defined behavior) for C90/C99? I have never seen…
tanascius
  • 50,043
  • 21
  • 106
  • 130