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
2168
votes
26 answers

How to write a switch statement in Ruby

How do I write a switch statement in Ruby?
readonly
  • 306,152
  • 101
  • 198
  • 201
1717
votes
44 answers

Replacements for switch statement in Python?

I want to write a function in Python that returns different fixed values based on the value of an input index. In other languages I would use a switch or case statement, but Python does not appear to have a switch statement. What are the…
Michael Schneider
  • 17,850
  • 3
  • 20
  • 14
1024
votes
17 answers

Why can't I use switch statement on a String?

Is this functionality going to be put into a later Java version? Can someone explain why I can't do this, as in, the technical way Java's switch statement works?
Alex Beardsley
  • 19,982
  • 13
  • 48
  • 67
1014
votes
23 answers

Why can't variables be declared in a switch statement?

I've always wondered this - why can't you declare variables after a case label in a switch statement? In C++ you can declare variables pretty much anywhere (and declaring them close to first use is obviously a good thing) but the following still…
Rob
  • 70,036
  • 53
  • 151
  • 189
895
votes
21 answers

Switch statement multiple cases in JavaScript

I need multiple cases in switch statement in JavaScript, Something like: switch (varName) { case "afshin", "saeed", "larry": alert('Hey'); break; default: alert('Default case'); break; } How can I do that? If…
Afshin Mehrabani
  • 28,405
  • 26
  • 117
  • 186
636
votes
20 answers

Multiple cases in switch statement

Is there a way to fall through multiple case statements without stating case value: repeatedly? I know this works: switch (value) { case 1: case 2: case 3: // Do some stuff break; case 4: case 5: case 6: // Do…
theo
  • 7,441
  • 3
  • 20
  • 22
531
votes
2 answers

What is the Python equivalent for a case/switch statement?

I'd like to know, is there a Python equivalent for the case statement such as the examples available on VB.net or C#?
John Alley
  • 5,461
  • 3
  • 11
  • 4
388
votes
13 answers

Switch statement fallthrough in C#?

Switch statement fallthrough is one of my personal major reasons for loving switch vs. if/else if constructs. An example is in order here: static string NumberToWords(int number) { string[] numbers = new string[] { "", "one", "two",…
Matthew Scharley
  • 115,776
  • 51
  • 189
  • 215
384
votes
14 answers

Is "else if" faster than "switch() case"?

I'm an ex Pascal guy, currently learning C#. My question is the following: Is the code below faster than making a switch? int a = 5; if (a == 1) { .... } else if(a == 2) { .... } else if(a == 3) { .... } else if(a == 4) { …
Ivan Prodanov
  • 31,952
  • 74
  • 168
  • 243
353
votes
31 answers

Is there a better alternative than this to 'switch on type'?

Seeing as C# can't switch on a Type (which I gather wasn't added as a special case because is relationships mean that more than one distinct case might apply), is there a better way to simulate switching on type other than this? void Foo(object…
xyz
  • 25,370
  • 28
  • 99
  • 125
344
votes
4 answers

Case statement with multiple values in each 'when' block

The best way I can describe what I'm looking for is to show you the failed code I've tried thus far: case car when ['honda', 'acura'].include?(car) # code when 'toyota' || 'lexus' # code end I've got about 4 or 5 different when…
Nick
  • 8,822
  • 6
  • 41
  • 65
341
votes
12 answers

Using two values for one switch case statement

In my code, the program does something depending on the text entered by the user. My code looks like: switch (name) { case text1: { //blah break; } case text2: { //blah break; …
Ankush
  • 5,687
  • 8
  • 26
  • 42
321
votes
7 answers

Java: using switch statement with enum under subclass

First I'll state that I'm much more familiar with enums in C# and it seems like enums in java is a quite mess. As you can see, I'm trying to use a switch statement @ enums in my next example but I always get an error no matter what I'm doing. The…
Popokoko
  • 5,473
  • 16
  • 40
  • 54
296
votes
24 answers

Is it possible to use the instanceof operator in a switch statement?

I have a question of using switch case for instanceof object: For example: my problem can be reproduced in Java: if(this instanceof A) doA(); else if(this instanceof B) doB(); else if(this instanceof C) doC(): How would it be…
olidev
  • 17,514
  • 49
  • 124
  • 187
281
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
  • 6,333
  • 7
  • 29
  • 36
1
2 3
99 100