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
2285
votes
28 answers

How to write a switch statement in Ruby

How do I write a switch statement in Ruby?
readonly
  • 343,444
  • 107
  • 203
  • 205
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,929
  • 3
  • 24
  • 14
1145
votes
26 answers

Switch statement for 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
  • 33,262
  • 29
  • 136
  • 201
1116
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
  • 76,700
  • 56
  • 158
  • 197
1047
votes
14 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
  • 20,988
  • 15
  • 52
  • 67
789
votes
2 answers

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

Is there a Python equivalent for the switch statement?
John Alley
  • 8,067
  • 3
  • 14
  • 10
735
votes
26 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
  • 8,501
  • 3
  • 23
  • 22
536
votes
7 answers

When to favor ng-if vs. ng-show/ng-hide?

I understand that ng-show and ng-hide affect the class set on an element and that ng-if controls whether an element is rendered as part of the DOM. Are there guidelines on choosing ng-if over ng-show/ng-hide or vice-versa?
Patrice Chalin
  • 15,440
  • 7
  • 33
  • 44
441
votes
17 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
  • 6,767
  • 8
  • 30
  • 42
420
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
  • 34,634
  • 78
  • 176
  • 248
417
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
  • 127,823
  • 52
  • 194
  • 222
405
votes
32 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
  • 27,223
  • 29
  • 105
  • 125
399
votes
8 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
  • 6,413
  • 16
  • 47
  • 58
385
votes
6 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
  • 9,493
  • 8
  • 43
  • 66
356
votes
29 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
  • 20,058
  • 51
  • 133
  • 197
1
2 3
99 100