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

How can I implement a switch statement inside a Column widget? FLUTTER

I would like to put a switch statement inside of my Column to decide what mix of children widgets to buildin my FLUTTER APP. In my specific case I have created an enum with 4 different possible states (AnimationEnum.None, AnimationEnum.etc), each…
Kdon
  • 892
  • 6
  • 19
3
votes
3 answers

PHP Translating a ENUM in switch case not working

echo $state; //debug switch($state){ case "Sleeping": $label = "Is Sleeping"; case "Running": $label = "Is Running"; default: $label = "Could not…
abinmorth
  • 527
  • 2
  • 8
  • 25
3
votes
1 answer

Dart - Map vs Switch statement performance

In our flutter project, with use a lot of enums and extensions. Some of us write switch statements, some others use maps. I was wondering what practice is considered the best one in terms of time, memory etc ? enum Animal { cat, dog, bird } Switch…
Valentin Vignal
  • 6,151
  • 2
  • 33
  • 73
3
votes
3 answers

How to create a dynamic PHP switch statement from MySQL query

This is for adding images, categories and comments from an email to a photo gallery I'm working on. I'm pulling the first two found category names out of an email message body and need to assign the category id to $cat variable for insertion into…
Mark
  • 467
  • 1
  • 6
  • 21
3
votes
2 answers

Bootstrap 5 text BEFORE switch-checkbox (without extra addons/plugins)

I use Bootstrap 5 and I wand to use a switch-toggle. I don't want to use any other addons, but only with bootstrap. How to put a Text BEFORE the toggle switch. This does not work:
off
Akkie
  • 41
  • 1
  • 6
3
votes
4 answers

converting an if statement to a switch statement

How can I convert the following if=statement to a switch-statement WITHOUT needing to create a case for every number between that interval (41-49)? Is it possible? if (num < 50 && num > 40) { printf("correct!"); }
wadafarfar
  • 41
  • 2
3
votes
2 answers

How to return multiple values or a tibble from case_when()?

I can't find a way to return multiple values (columns) or just a tibble from case_when(). input <- tibble(a = c(1, 2, 3)) input %>% mutate( case = case_when( a == 1 ~ tibble(x = "case1", y = "c1"), a == 2 ~ tibble(x =…
Piotr K
  • 943
  • 9
  • 20
3
votes
2 answers

Switch case formatting issue with SwiftLint

My switch case is auto formatted when i do Cntrl + i , like below switch someBool { ↓case true: print("success") ↓case false: print("failed") } but its throws a lint warning Switch and Case Statement Alignment Violation:…
Pavan kumar C
  • 393
  • 1
  • 4
  • 15
3
votes
4 answers

What is (or should be) the cyclomatic complexity of a virtual function call?

Cyclomatic Complexity provides a rough metric for how hard to understand a given function is, or how much potential for containing bugs it has. In the implementations I've read about, usually all of the basic control flow constructs (if, case,…
3
votes
2 answers

Switch case with or expression on enum does not evaluate as expected

I am trying to switch on an enum so that the correct code is run. I have made a typescript playground that showcases a small example of what I am trying to accomplish. In the provided example I am trying to understand why Test1 will not print "B".…
darophi
  • 456
  • 5
  • 15
3
votes
3 answers

Case statement with braces

Why does a case statement allow declarations within braces but not without them? For example, the following is not OK switch (op->name) { case 0: int a = 2; case 1: int b = 3; } But the following is OK: switch (op->name)…
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
3
votes
4 answers

Help with do/while and switch statements in C

Trying to compile a simple switch statement with 5 choices. 1-4 produce calculations and output while #5 exits the program. I made a do/while loop so if choice 5 is entered the program will end. I get an error: 4_19.c: In function…
J-e-L-L-o
  • 315
  • 4
  • 9
3
votes
3 answers

php - split switch cases in different files

I have a php file in which i am using a really very long switch case. I want to split the cases in different files (keep logically connected cases in 1 file). EDIT: Sorry everyone it was my code that was causing problem. The switch case was working…
user427969
  • 3,836
  • 6
  • 50
  • 75
3
votes
3 answers

Bad practice or frowned upon to use a comma to separate cases? Switch case statement Java

is it considered bad practice to write a switch case statement with a comma such as this: switch(name) { case 'a', 'A' : break; } Rather than switch(name) { case 'a': case 'A': break; } Just curious as my code seems to run fine either way but I…
AWcode
  • 45
  • 4
3
votes
0 answers

How to merge two cases with arguments in a switch statement

I would like to merge two cases with arguments in a switch statement in C#. It is easy to merge cases in case of chars, string and integers: switch (int) { case 1: case 2: { } } and a lot of info can be found…
Casper Dijkstra
  • 1,615
  • 10
  • 37