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 .

10485 questions
3
votes
2 answers

What is the alternative of using several elseif nested loops in PHP

What is the best alternative of using several (23 in my case) if-elseif nested loops in PHP?
PHP Developer
  • 947
  • 4
  • 11
  • 32
3
votes
3 answers

Appropriate use of Multi-level Break in Java

I was recently coding a small java program (as design for an 8086 Assembler program) and I wound up in an interesting position -- I needed to exit out of a while loop from an inner switch-statement, something like this (pseudocode,…
Raven Dreamer
  • 6,600
  • 13
  • 61
  • 96
3
votes
5 answers

If conditional tree to switch statement efficiency troubles

I've been trying to optimize functions with long series of if statements. I'd though I'd found a solution using switch statements as a replacement, but upon testing and disassembly, I found out that they just complicated the matter. Heres some code…
user4578093
  • 221
  • 1
  • 2
  • 10
3
votes
4 answers

Switch case with multiple values

Is there something like array of case statement and put it as single case statement into switch- suppose String[] statements={"height","HEIGHT"}; and then switch(code){ case statements: //code here break; case something_else: break; } so…
dsk
  • 517
  • 1
  • 6
  • 18
3
votes
3 answers

Why is `switch` so slow?

In a bytecode interpreting loop, after several tests, I'm surprised to see that using switch is the worst choice to make. Making calls to a function pointer array, or using gcc's computed goto extension is always 10~20% faster, the computed goto…
xiver77
  • 6,073
  • 4
  • 23
  • 58
3
votes
6 answers

Compare if a-z or A-Z and give output with switch JavaScript function

I'm learning JavaScript. Very new and know basic. I'm playing with various options of JavaScript. I'm comparing a-z (lower case) and A-Z (upper case) from user input. and giving an answer base on the input. Normally i can do this with this long…
3
votes
2 answers

Alternative to Ruby Case statement

I am currently writing a program for a banking administration system using Ruby. One of the capabilities of this system is that it can create a new account, accounts can be one of six types. I have the following method in my controller to cater for…
Phil Brockwell
  • 467
  • 5
  • 22
3
votes
1 answer

Does the underscore symbol ignore or check for nullity in a switch statement in Swift?

I have a switch statement in Swift like this: switch tuple { case (let someObject, let current, nil): return true // Other cases... } The tuple is of type (SomeObject?, SomeObject, SomeObject?), and what I'm saying in English is:…
MLQ
  • 12,329
  • 12
  • 79
  • 130
3
votes
1 answer

Generate const strings at compile time for switch cases

We have in a legacy code base on .net 4.5 (important) a static class which defines many const string values of object types (means the values of x.GetType().ToString()) mainly for usage in switch statements. This is especially bad because certain…
Sam
  • 561
  • 1
  • 5
  • 16
3
votes
1 answer

Binary operator '~=' cannot be applied to two UmRet operands

I'm working on integrating an IDTech swiper into my app and I've gotten pretty far along, added the library, registered notifications, unregistered them, and now I'm working on a function that connects the reader. I can't seem to figure out what I'm…
user1940321
3
votes
1 answer

Why final Byte as a case in switch statement doesn't compile?

byte a = 125; final byte b = 2; final Byte c = 3; switch (a) { case b: // works fine break; case c: // Constant Expression required break; } Since cis a final variable, isn't it a compile time constant and hence a valid case label?
paidedly
  • 1,403
  • 1
  • 12
  • 21
3
votes
4 answers

Swap background-color of a specific div with switch/case

I'm trying to swap the background-color of some specific divs basing on their content. Their background-color should swap when their input is like "Lifestyle" or "Politics" or "Economy" or "Local" or "Sports" or "News". var colorInput =…
3
votes
2 answers

Can I use an array in a case statement?

I would like to use an array of const int in a switch/case statement in C++. Is it possible? So far I've tried something like: int main() { int const tab[3] = {1,2,3}; int value(2); switch(value) { case tab[1]: …
Alex
  • 33
  • 1
  • 4
3
votes
2 answers

Cannot switchTo frames -- keep getting AttributeError

my first time posting. My apologies in advance for any missteps. I am writing in Python, using Selenium, trying to scrape some information from some webpages. I can not seem to solve this puzzle after two days of searching-trying-searching. My…
Christine
  • 109
  • 8
3
votes
1 answer

Best practice to create two-level deep selection menu in Java console

I'm about to create an application that runs off the java console as opposed to the GUI, and I had a question about the best way to approach displaying menus with another level of a submenu. I've quickly typed up the below code to sort of give you…
Tyrx
  • 91
  • 1
  • 7
1 2 3
99
100