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
4 answers

Creating console command list - how to do it right with Java

For a homework I code in Java(with which I'm a newbie), I bump into the problem of creating a console command list. The user will be confronted with a set of commands, among which s/he will choose his/her choice with a number. Something like…
kolistivra
  • 4,229
  • 9
  • 45
  • 58
3
votes
2 answers

I have a C# switch and I need to call a method before each case returns. Is there any way I could simplify this?

I have this code which I refactored to use a switch but it still looks very clumsy. Now I am looking for a way to simplify it. public async Task CheckAvailability() { switch (Settings.Mode) { case…
Alan2
  • 23,493
  • 79
  • 256
  • 450
3
votes
1 answer

Nightmare on Segue street

What the app should do: 1) The user types a word into a textField and taps the corresponding button 2) The app should segue to another scene containing two labels. One should display the word typed by the user, the other identifies the button…
rattletrap99
  • 1,469
  • 2
  • 17
  • 36
3
votes
4 answers

Refactor big switch case combined with if clauses

So I'm having something like this: switch (data){ case "one" : if (version1) createDataOneV1(); else createDataOnetV2(); break; case "two": if(version1) …
UnguruBulan
  • 890
  • 4
  • 12
  • 24
3
votes
3 answers

Return exception in switch statement and then throw the called method

I have two methods private String handleResponse(HttpResponse httpResponse){ if (response.getStatusCode() / 100 == 2) { return response.getEntity().toString() } else { throw handleException(response.getStatusCode()); …
MarkyMark
  • 199
  • 1
  • 15
3
votes
2 answers

segmented control always return 0

I'm using a TabBar app with a navigation item that includes a UISegmentedControl. I've connected a method when the event "value changed" is caught. The method always catch 0 as SegmentIndex... Here's my header file : #import…
3
votes
4 answers

Syntax of switch statement in C?

I am trying to understand the switch statement in C (I am using gcc from Ubuntu v16.04). I am able to understand its semantics, but have the following 2 questions about its syntax: I have noticed after reading a few examples of usage of switch…
Anon
  • 381
  • 1
  • 2
  • 13
3
votes
4 answers

php if and elseif statements with multiple/more than 3 conditions

I want to assign random profile pictures to users when they register on a website. I have a problem with my code. If I limit the "if and elseif" statements to 2 conditions, it works well. But when I go over 2 (in this case I have 10 conditions), the…
Shea
  • 41
  • 3
3
votes
1 answer

With GCC switch case ranges, is it possible to construct a single case that ranges [A-Za-z]?

Pretty straight forward question. GCC has Case ranges that allow for things like this: switch (c.toLatin1()) { default: { foo(); break; } case 'A' ... 'Z': { bar(); break; } case 'a' ... 'z': { bar(); …
Anon
  • 2,267
  • 3
  • 34
  • 51
3
votes
2 answers

c++ default not working in switch statement

Fairly new to programming here. I can't get my default code in the switch statement to work. I want it to display an error and exit as soon as any data input that's not in the range is entered. For some reason, it goes to the next cout line after…
ThePete
  • 33
  • 3
3
votes
7 answers

Is there a way of performing some code within a Switch statement that executes only if any of the cases have been passed?

Is there a way that I can execute the same line of code for each "Case" but only have to type it in once instead of having the same code specified for all Cases ? switch (SomeTest) { case "test1": { …
cyberbobcat
  • 1,169
  • 1
  • 18
  • 34
3
votes
2 answers

Run correct method based on file name (C#)

I'm checking name of the file and return TRUE if it's correct: bool name_FORD = file.Contains("FORD"); bool name_KIA = file.Contains("KIA"); bool name_BMW = file.Contains("BMW"); Based on this I want to have switch and run correct method. But…
4est
  • 3,010
  • 6
  • 41
  • 63
3
votes
4 answers

Switch String with Enum variables

I have got an Enum with different values and want to switch a string variable. Now I hit a wall trying to convert the Enum values to Strings, that I can use as case constant. My best try was to convert the Enum to a String array, but the switch…
3
votes
2 answers

Perl & Net::SNMP::Interfaces::Details, how to get mac address?

for my internship i have to code a network supervisor. I'm writting perl scripts to find all informations (speed, mac address, duplex...) from an interface name on the switch. There is a function "ifPhysAddress" in this module, but it returns the…
eouti
  • 5,338
  • 3
  • 34
  • 42
3
votes
2 answers

Why `switch(null)` is a compile error but `switch(str)` is fine with str being `static final String str = null;`?

While switch(null) is a compile error but switch(str) is fine (str being static final String str = null;). Isn't static final String str = null; a compile-time constant which shall be substituted into switch(str) at compile time and thus be…
Code Complete
  • 3,146
  • 1
  • 15
  • 38
1 2 3
99
100