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

Why is declaring a variable in switch statement allowed? but not declaration + initialization?

Why is declaring + initializing a variable inside a case of a switch statement not allowed & gives an error, but if it is declared it on one line then assigned a value on another, it compiles? Why the variable that was declared in a previous case…
topcat
  • 177
  • 3
  • 17
3
votes
3 answers

C++ Switch Statement inputs

I am writing a C++ program that prompts the user for an input and then keeps track of how many times that input is entered. I am currently using a do-while loop and a switch statement. The part I am having trouble with is the switch statement. I…
HarryJEST
  • 97
  • 3
  • 9
3
votes
2 answers

Is there a way to minimise if and if-else condition

I have written a code which is working fine but I'am using too many If and else-if conditions. Is there a way to minimise it? Based on the integer values shippingStatus, invoiceStatus and paymentStatus values should change. int qtyOrdered =…
3
votes
4 answers

Simplify Massive Match Case - Scala

So in one of the places we have this huge variable match case statement. Containing almost 150 distinct case statements Looks horrible. I want to break it up into smaller functions, I can group the matches into say 10 pieces and then the number case…
3
votes
1 answer

Center each label around a switch that has two or more lines

I am working with a switch, and because the actual label is wide, I want to break each labeled part. The issue is that I cannot find a way to break it such that it aligns with the switch widget. I have tried to make a new line with both
and…
WFTsai
  • 95
  • 1
  • 9
3
votes
2 answers

How do you use `switch` in C# to conditionally branch based on only a type parameter?

My context is that I'm building a simple factory method for creating instances of derived types of a given base type. The factory method only takes a type parameter, i.e. doesn't have any arguments. This is obviously possible with an if-else if…
rory.ap
  • 34,009
  • 10
  • 83
  • 174
3
votes
5 answers

switch case: case label does not reduce to an integer constant

I am perfectly aware of the mechanism behind the switch statement and why an integer constant is required. What I don't undestand is why the following case label is not considered an integer constant. What is it then? A non-existing variable? Can…
madmurphy
  • 1,451
  • 11
  • 20
3
votes
4 answers

Local variables in Java switch control

I found this case a bit peculiar: int x = 1; switch(x){ case 0 : boolean b = false; break; case 1 : b = true; //will compile just right System.out.println(b); //will…
christianleroy
  • 1,084
  • 5
  • 25
  • 39
3
votes
1 answer

Behavior of Objective-C enum "exhaustive" switch without a default case

In Swift, the init(rawValue:) system ensures that casting an Int to an enum either results in a valid enum case or nil. There is no such safety in Objective-C, where an invalid enum member can be created by casting a non-member "rawValue". typedef…
pkamb
  • 33,281
  • 23
  • 160
  • 191
3
votes
3 answers

Fall-through if default is in middle of switch case?

I have a question. In a switch statement, is default tested for last even if it isn't last? If so, in the following code snippet: int i = 6; int a=0, b=0, c=0; switch (i) { case 1: a++; case 2: default: case 3: b++; …
Diana
  • 95
  • 1
  • 8
3
votes
2 answers

integer to word conversion in java using map continue question

This is a probable answer of my question in stack overflow.Integer to word conversion At first I have started with dictionary. Then I came to know it is obsolete. So now I use Map instead of dictionary. My code is work well for number till…
Encipher
  • 1,370
  • 1
  • 14
  • 31
3
votes
7 answers

Is there any alternatives to a huge switch, with multiple cases and fallthrough in JavaScript?

I need to map some user generated fields to something the system I'm working on can recognize. For this we want to provide a certain amount of freedom for users, and offer five or so options for each of our fields. So far we have a switch which…
Anders Jensen
  • 330
  • 3
  • 20
3
votes
3 answers

Trying to use switch statement insted of if else to find out which value is bigger or smaller

I am a beginner in learning c# (and any coding language) I am trying to use switch statement instead of if else. this is the working if else statement private void RunScript(int a, int b, ref object A) { if (a < b) { Print("a is…
Arceps
  • 39
  • 1
3
votes
2 answers

How to code a switch statement to test DialogResult and provide fall through logic

I'm getting strange results testing the return value from a function. This code is inside a for loop: DialogResult result = EvalReturnFromWS(returnMsg); switch (result) { case DialogResult.Yes: case DialogResult.No: …
John Adams
  • 4,773
  • 25
  • 91
  • 131
3
votes
3 answers

switch statement with multiple conditions in r

I want to write a switch statement in r with three conditions but can't seem to get it to work. What am I doing wrong? # assigning some values test.type <- "p" var.equal<- TRUE paired <- FALSE # preparing text for which p-value adjustment method…
Indrajeet Patil
  • 4,673
  • 2
  • 20
  • 51
1 2 3
99
100