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
86
votes
1 answer

What is causing this: Cannot jump from switch statement to this case label

This is a switch statement that I am getting errors on: switch (transaction.transactionState) { case SKPaymentTransactionStatePurchasing: // show wait view here statusLabel.text = @"Processing..."; …
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
85
votes
6 answers

Java Switch Statement - Is "or"/"and" possible?

I implemented a font system that finds out which letter to use via char switch statements. There are only capital letters in my font image. I need to make it so that, for example, 'a' and 'A' both have the same output. Instead of having 2x the…
GlassZee
  • 1,117
  • 2
  • 11
  • 22
84
votes
6 answers

How do I use properly CASE..WHEN in MySQL

Here is a demo query, notice it is very simple, Fetches only where base_price is 0, And still, it chooses the condition 3: SELECT CASE course_enrollment_settings.base_price WHEN course_enrollment_settings.base_price = 0 THEN 1 WHEN…
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
83
votes
1 answer

Does switch case order affect speed?

I've tried to google this, but had no luck. I have a very big switch, and some cases are obviously more common than others. So I would like to know if the order is really held as it is and the "upper" cases get tested before the "lower", therefore…
MightyPork
  • 18,270
  • 10
  • 79
  • 133
83
votes
9 answers

Which is Faster and better, Switch Case or if else if?

Which is the better and fastest methods : if or switch ? if(x==1){ echo "hi"; } else if (x==2){ echo "bye"; } switch(x){ case 1 ... break; default; }
Ballu Rocks
  • 1,206
  • 3
  • 13
  • 15
82
votes
2 answers

Case in protected switch

Possible Duplicate: When converting a project to use ARC what does “switch case is in protected scope” mean? Got the following xcode: But when i try to put something in case 1 (or empty) it's giving me an error? Weird problem because i dont know…
Blazer
  • 14,259
  • 3
  • 30
  • 53
82
votes
8 answers

What if I don't write default in switch case?

int a = 10; switch(a){ case 0: printf("case 0"); break; case 1: printf("case 1"); break; } Is the above code valid? If I am sure that int a will not have any other value than 1 and 0, can I avoid default? What if in any case a value…
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
82
votes
4 answers

C# Switch statement with/without curly brackets.... what's the difference?

Has C# always permitted you to omit curly brackets inside a switch() statement between the case: statements? What is the effect of omitting them, as javascript programmers often do? Example: switch(x) { case OneWay: { …
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
81
votes
12 answers

Use string.Contains() with switch()

I'm doing an C# app where I use if ((message.Contains("test"))) { Console.WriteLine("yes"); } else if ((message.Contains("test2"))) { Console.WriteLine("yes for test2"); } There would be any way to change to switch() the if() statements?
pmerino
  • 5,900
  • 11
  • 57
  • 76
81
votes
4 answers

What does the new keyword "yield" mean in Java 13?

Java 13 introduced the yield keyword for switch expressions. How can I use it and what's the difference to a default return or break value?
zerocewl
  • 11,401
  • 6
  • 27
  • 53
81
votes
14 answers

Status "S" in Subversion

At some point all files in my working copy got marked with "S" symbol as shown below: $ svn st M S AclController.php S InstallationController.php S CustomerController.php S RedirController.php S IndexController.php …
Michał Niedźwiedzki
  • 12,859
  • 7
  • 45
  • 47
80
votes
4 answers

Performance of if-else, switch or map based conditioning

I was wondering about the performances of the following implementations of conditional structs in javascript. Method 1: if(id==="camelCase"){ window.location.href = "http://www.thecamelcase.com"; }else if (id==="jsFiddle"){ …
aditya_gaur
  • 3,209
  • 6
  • 32
  • 43
80
votes
9 answers

Throwing exceptions in switch statements when no specified case can be handled

Let's say we have a function that changes a password for a user in a system in an MVC app.: public JsonResult ChangePassword (string username, string currentPassword, string newPassword) { switch…
CantSleepAgain
  • 3,743
  • 3
  • 21
  • 18
79
votes
6 answers

JavaScript, Typescript switch statement: way to run same code for two cases?

Is there a way to assign two different case values to the same block of code without copy and pasting? For example, below 68 and 40 should execute the same code, while 30 is not related. case 68: //Do something break; case 40: //Do the same…
nipponese
  • 2,813
  • 6
  • 35
  • 51
78
votes
2 answers

switch "transfer of control bypasses initialization of:" when calling a function

I get a "transfer of control bypasses initialization of:" error when i try to build the following switch: switch (retrycancel) { case 4: //The user pressed RETRY //Enumerate all visible windows and store handle and caption in…
Lumpi
  • 2,697
  • 5
  • 38
  • 47