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
155
votes
7 answers

Variable declaration in a C# switch statement

Why is it that in a C# switch statement, for a variable used in multiple cases, you only declare it in the first case? For example, the following throws the error "A local variable named 'variable' is already defined in this scope". switch (Type) { …
Jeremy
  • 9,023
  • 20
  • 57
  • 69
153
votes
16 answers

Java switch statement multiple cases

Just trying to figure out how to use many multiple cases for a Java switch statement. Here's an example of what I'm trying to do: switch (variable) { case 5..100: doSomething(); break; } versus having to do: switch (variable) { …
FunJavaCode
  • 1,541
  • 2
  • 9
  • 4
153
votes
5 answers

swift case falling through

Does swift have fall through statement? e.g if I do the following var testVar = "hello" var result = 0 switch(testVal) { case "one": result = 1 case "two": result = 1 default: result = 3 } is it possible to have the same code executed…
Bilal Syed Hussain
  • 8,664
  • 11
  • 38
  • 44
150
votes
17 answers

C# switch statement limitations - why?

When writing a switch statement, there appears to be two limitations on what you can switch on in case statements. For example (and yes, I know, if you're doing this sort of thing it probably means your object-oriented (OO) architecture is iffy -…
ljs
  • 37,275
  • 36
  • 106
  • 124
149
votes
9 answers

Why doesn't String switch statement support a null case?

I am just wondering why the Java 7 switch statement does not support a null case and instead throws NullPointerException? See the commented line below (example taken from the Java Tutorials article on switch): { String month = null; switch…
Prashant Bhate
  • 10,907
  • 7
  • 47
  • 82
147
votes
16 answers

Switch case: can I use a range instead of a one number

I want to use switch, but I have many cases, is there any shortcut? So far the only solution I know and tried is: switch (number) { case 1: something; break; case 2: other thing; break; ... case 9: .........; break; } What I hope I'm able to do…
user3022162
  • 1,499
  • 2
  • 10
  • 4
145
votes
10 answers

What is the relative performance difference of if/else versus switch statement in Java?

Worrying about my web application's performances, I am wondering which of "if/else" or switch statement is better regarding performance?
Anth0
  • 3,004
  • 6
  • 26
  • 36
139
votes
10 answers

switch case statement error: case expressions must be constant expression

My switch-case statement works perfectly fine yesterday. But when I run the code earlier this morning eclipse gave me an error underlining the case statements in color red and says: case expressions must be constant expression, it is constant I…
HeartlessArchangel
  • 1,737
  • 4
  • 13
  • 17
134
votes
5 answers

Why switch is faster than if

Lots of Java books describe the switch statement as being faster than the if else statement. But I did not find out anywhere why switch is faster than if. Example I have a situation I have to choose any one item out of two. I can use either…
user831722
131
votes
3 answers

Is using if (0) to skip a case in a switch supposed to work?

I have a situation where I would like for two cases in a C++ switch statement to both fall through to a third case. Specifically, the second case would fall through to the third case, and the first case would also fall through to the third case…
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
128
votes
6 answers

Using continue in a switch statement

I want to jump from the middle of a switch statement, to the loop statement in the following code: while (something = get_something()) { switch (something) { case A: case B: break; default: // get another…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
125
votes
8 answers

Java using enum with switch statement

I've looked at various Q&As on SO similar to this question but haven't found a solution. What I have is an enum which represents different ways to view a TV Guide... In the NDroid Application class static enum guideView { GUIDE_VIEW_SEVEN_DAY, …
Squonk
  • 48,735
  • 19
  • 103
  • 135
121
votes
3 answers

Is it safe to assume strict comparison in a JavaScript switch statement?

I have a variable that can either be boolean false, or an integer (including 0). I want to put it in a switch statement like: switch(my_var){ case 0: // Do something break; case 1: // Do something else …
Paul
  • 139,544
  • 27
  • 275
  • 264
120
votes
3 answers

Declaring variables inside a switch statement

I saw a few answers to this issue, and I get it — you can't declare and assign variables inside a switch. But I'm wondering if the following is correct at throwing an error saying error: expected expression before 'int' Code: switch (i) { …
dizy
  • 7,951
  • 10
  • 53
  • 54
118
votes
6 answers

If vs. Switch Speed

Switch statements are typically faster than equivalent if-else-if statements (as e.g. descibed in this article) due to compiler optimizations. How does this optimization actually work? Does anyone have a good explanation?
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316