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

Why does declaration is valid, and intialization is not in switch statement

I've already read this " Why can't variables be declared in a switch statement? ". However, I still can't understand why does this work: switch(n){ case VAL: int a; ... break; case OTHER_VAL: a = 12 ... break; } suppose to…
0_NULL
  • 73
  • 1
  • 7
3
votes
2 answers

Using parentheses to group expressions in case

I would like to group expressions with () in case, like so: case a in '*(a|b)*') printf '%s\n' 'something something';; esac This has not yielded any success though. I have also tried: *(a|b)* *'('a|b')'* None of which I had success with.
nacn
  • 25
  • 4
3
votes
2 answers

How can I simplify a switch statement that executes a templated function?

Consider the following program: #include #include #include class A; class B; class C; template void func1(); template<> void func1() { std::cout << "func 1 A"; } template<> void func1(){ …
User12547645
  • 6,955
  • 3
  • 38
  • 69
3
votes
1 answer

PHP: How to use same logic for multiple cases in Switch statement

I have a function that returns me the response based on the value of the input. The code is something like this $a = 5; switch ($a) { case 1: $b = 1; break; case 2 || 3 || 4: $b = 2; break; case 5: $b = 3; break; } return…
Saeesh Tendulkar
  • 638
  • 12
  • 30
3
votes
3 answers

switch statement with nested if/else gives multiple outputs in c++

I'm writing a pretty simple program for my first-year C++ class, where I utilize switch statements to get a correct output depending on what two numbers and what special character is used between them (adding, subtracting, multiplying, dividing).…
Chopskie
  • 31
  • 1
3
votes
4 answers

C++ switch statement - jump between cases

In C++, can I do something like the code below? In a specific case, if some conditions are met, can I jump to another case? switch (tag) { case 1: { // do something break; } case 2: { if (/* condition */) { …
Xuhang
  • 445
  • 5
  • 20
3
votes
2 answers

A switch statement without case: or default: labels

cppreference.com stats that the form of the switch statement is: attr switch ( condition ) statement and that statement is any statement. So, what will happen if I didn't put any case: or default: labels in statement, as: switch (1) {std::cout <<…
Mason
  • 501
  • 3
  • 11
3
votes
2 answers

Avoid copying switch statments

Can this code be refactored to avoid copying switch statements? enum class Animal { Cat, Dog, Fish}; float GetMaxSpeed(Animal a) { switch (a) { case Animal::Cat: return 30; case Animal::Dog: return 40; …
Ivana
  • 643
  • 10
  • 27
3
votes
3 answers

Is it good a good practice to stringify array?

Let's say I have an Array consist of Boolean. myArray = [false,false,false] And I will need to do different stuff according to every status. ex : [false,true,false], [false,false,true], [true,true,false], ... and so on. Usually this kind of…
Cerceis
  • 770
  • 3
  • 14
3
votes
3 answers

SwitchButton (in custom view) value is not checked after rotation

I need to create custom view - TextView and Switch button. I have custom view: public class CustomTextWithSwitch extends LinearLayout implements View.OnClickListener { private CustomTextWithSwitchBinding binding; private boolean defaultValue; …
Tom11
  • 2,419
  • 8
  • 30
  • 56
3
votes
2 answers

Toggle change 2 DIV width at a time + localStorage

function myFunction() { var element = document.getElementById("one1"); element.classList.toggle("one2"); var element = document.getElementById("two1"); …
Hélène
  • 33
  • 4
3
votes
3 answers

C switch statement , variable error(For a calculator program source code)

Greetings , today when I try something new(Concept and style that suddenly pop out in my mind) , I encountered a few problems which I don't understand why it happened. The Code // This program would get two numbers from user , and request for an…
caramel1995
  • 2,968
  • 10
  • 44
  • 57
3
votes
3 answers

Search in array or switch? For a cipher by substitution

I'm coding a classical cipher by substitution in C++, using all the printable characters in ASCII, and I'm wondering which is faster? A search in an array (edit: a non associative one, just something like letters[] = {'a', 'b', ...); (linear or…
Tae
  • 1,665
  • 5
  • 24
  • 45
3
votes
4 answers

Switch statement instead of a ton of if statements

I'm currently working with notifications and wanna print out an identifier. How can I replace all if-statements with one switch? Here is my enum that keeps all identifier with corresponding string value: enum NotificationIdentifier:String { …
3
votes
2 answers

Programmable Wifi Relay / Power Switch?

I'm toying with the idea of a pet project.. I wanna make our coffee machine remote controlled by a web interface. The idea is to hit a "make me coffee button" and have it poll the router get the MAC address list to see who's in the office and send…
Daniel Upton
  • 5,561
  • 8
  • 41
  • 64