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
221
votes
10 answers

How to implement switch-case statement in Kotlin

How to implement equivalent of following Java switch statement code in Kotlin? switch (5) { case 1: // Do code break; case 2: // Do code break; case 3: // Do code break; }
Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25
211
votes
7 answers

Does a break statement break from a switch/select?

I know that switch/select statements break automatically after every case. I am wondering, in the following code: for { switch sometest() { case 0: dosomething() case 1: break default: dosomethingelse() …
Matt
  • 21,026
  • 18
  • 63
  • 115
210
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
  • 63,815
  • 23
  • 109
  • 159
209
votes
12 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
  • 53,078
  • 22
  • 114
  • 136
195
votes
10 answers

Lesser than or greater than in Swift switch statement

I am familiar with switch statements in Swift, but wondering how to replace this piece of code with a switch: if someVar < 0 { // do something } else if someVar == 0 { // do something else } else if someVar > 0 { // etc }
Pieter
  • 2,621
  • 4
  • 19
  • 26
192
votes
10 answers

Using Case/Switch and GetType to determine the object

Possible Duplicate: C# - Is there a better alternative than this to ‘switch on type’? If you want to switch on a type of object, what is the best way to do this? Code snippet private int GetNodeType(NodeDTO node) { switch (node.GetType()) …
user29964
  • 15,740
  • 21
  • 56
  • 63
185
votes
23 answers

Advantage of switch over if-else statement

What's the best practice for using a switch statement vs using an if statement for 30 unsigned enumerations where about 10 have an expected action (that presently is the same action). Performance and space need to be considered but are not critical.…
Zing-
  • 2,087
  • 2
  • 13
  • 12
182
votes
8 answers

Control cannot fall through from one case label

I am trying to write a switch statement that would type the search term in the search field depending on whichever search textbox is present. I have the following code. But I am getting a "Control cannot fall through from one case label"…
Maya
  • 7,053
  • 11
  • 42
  • 53
179
votes
14 answers

SQL Switch/Case in 'where' clause

I tried searching around, but I couldn't find anything that would help me out. I'm trying to do this in SQL: declare @locationType varchar(50); declare @locationID int; SELECT column1, column2 FROM viewWhatever WHERE CASE @locationType WHEN…
Miles
  • 5,646
  • 18
  • 62
  • 86
176
votes
8 answers

Switch on ranges of integers in JavaScript

I want to do something like this switch (this.dealer) { case 1-4: // Do something. break; case 5-8: // Do something. break; case 9-11: // Do something. …
Jaanus
  • 16,161
  • 49
  • 147
  • 202
172
votes
14 answers

Can Objective-C switch on NSString?

Is there a more intelligent way to rewrite this? if ([cardName isEqualToString:@"Six"]) { [self setValue:6]; } else if ([cardName isEqualToString:@"Seven"]) { [self setValue:7]; } else if ([cardName isEqualToString:@"Eight"]) { [self…
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
171
votes
12 answers

Switch statement fall-through...should it be allowed?

For as long as I can remember I have avoided using switch statement fall-through. Actually, I can't remember it ever entering my consciousness as a possible way to do things as it was drilled into my head early on that it was nothing more than a bug…
Fostah
  • 11,398
  • 10
  • 46
  • 55
160
votes
10 answers

Is there any benefit to this switch / pattern matching idea?

I've been looking at F# recently, and while I'm not likely to leap the fence any time soon, it definitely highlights some areas where C# (or library support) could make life easier. In particular, I'm thinking about the pattern matching capability…
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
159
votes
7 answers

How add "or" in switch statements?

This is what I want to do: switch(myvar) { case: 2 or 5: ... break; case: 7 or 12: ... break; ... } I tried with "case: 2 || 5" ,but it didn't work. The purpose is to not write same code for different values.
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
159
votes
20 answers

How to break out of a loop from inside a switch?

I'm writing some code that looks like this: while(true) { switch(msg->state) { case MSGTYPE: // ... break; // ... more stuff ... case DONE: break; // **HERE, I want to break out of the loop itself** } } Is there…
jrharshath
  • 25,975
  • 33
  • 97
  • 127