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
104
votes
16 answers

Why do we need break after case statements?

Why doesn't the compiler automatically put break statements after each code block in the switch? Is it for historical reasons? When would you want multiple code blocks to execute?
unj2
  • 52,135
  • 87
  • 247
  • 375
103
votes
6 answers

Using {} in a case statement. Why?

What is the point with using { and } in a case statement? Normally, no matter how many lines are there in a case statement, all of the lines are executed. Is this just a rule regarding older/newer compilers or there is something behind that? int a =…
mahmood
  • 23,197
  • 49
  • 147
  • 242
103
votes
4 answers

How to use the switch statement in R functions?

I would like to use for my function in R the statement switch() to trigger different computation according to the value of the function's argument. For instance, in Matlab you can do that by writing switch(AA) case '1' ... case '2'…
Simon
  • 1,942
  • 5
  • 18
  • 22
102
votes
8 answers

Java switch cases: with or without braces?

Consider the following two snippets, with braces: switch (var) { case FOO: { x = x + 1; break; } case BAR: { y = y + 1; break; } } Without braces: switch (var) { case FOO: x = x + 1; break; case BAR: y = y…
cdmckay
  • 31,832
  • 25
  • 83
  • 114
102
votes
13 answers

Typescript enum switch not working

i have the following enum enum EditMode { View = 0, Edit = 1, Delete = 2 } Let's assume i have a variable of the enum type var editMode = EditMode.Edit; Why does the following code not work (goes straight to default)? switch (editMode)…
Mantzas
  • 2,463
  • 3
  • 25
  • 35
100
votes
1 answer

Elm Compiler running forever, computer just getting hot

I'm not sure what's causing this issue, but in a project, I'm building, the compiler is taking hours just to compile a module. The total size of my codebase is 352KB, but none of the modules are over 10KB large. I am using a Native port, but it's…
Athan Clark
  • 3,886
  • 2
  • 21
  • 39
99
votes
10 answers

What's the PowerShell syntax for multiple values in a switch statement?

I basically want to do this: switch($someString.ToLower()) { "y", "yes" { "You entered Yes." } default { "You entered No." } }
Micah
  • 111,873
  • 86
  • 233
  • 325
98
votes
5 answers

Why can't your switch statement data type be long, Java?

Here's an excerpt from Sun's Java tutorials: A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Classes and Inheritance) and a few special classes that "wrap" certain primitive…
Fostah
  • 11,398
  • 10
  • 46
  • 55
97
votes
9 answers

Switch in Laravel 5 - Blade

How can I use switch in blade templates? When I used: @switch($login_error) @case(1) `E-mail` input is empty! @break @case(2) `Password` input is empty! @break @endswitch in result I see this text as…
ventaquil
  • 2,780
  • 3
  • 23
  • 48
95
votes
2 answers

How to write a Ruby switch statement (case...when) with regex and backreferences?

I know that I can write a Ruby case statement to check a match against a regular expressions. However, I'd like to use the match data in my return statement. Something like this semi-pseudocode: foo = "10/10/2011" case foo when /^([0-9][0-9])/ …
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175
94
votes
1 answer

Why is GCC warning me about a fallthrough even when I use [[fallthrough]]?

In the following piece of code, I use the standard [[fallthrough]] attribute from C++1z to document that a fallthrough is desired: #include int main() { switch (0) { case 0: std::cout << "a\n"; …
s3rvac
  • 9,301
  • 9
  • 46
  • 74
93
votes
4 answers

How do use a Switch Case Statement in Dart

I am trying to understand how the switch is working in the dart. I have very simple code: methodname(num radians) { switch (radians) { case 0: // do something break; case PI: // do something else break; } } This…
Peter StJ
  • 2,297
  • 3
  • 21
  • 29
92
votes
3 answers

switch with var/null strange behavior

Given the following code: string someString = null; switch (someString) { case string s: Console.WriteLine("string s"); break; case var o: Console.WriteLine("var o"); break; default: …
budi
  • 6,351
  • 10
  • 55
  • 80
91
votes
2 answers

Do Go switch/cases fallthrough or not?

What happens when you reach the end of a Go case, does it fall through to the next, or assume that most applications don't want to fall through?
mcandre
  • 22,868
  • 20
  • 88
  • 147
89
votes
14 answers

How can I increment a variable without exceeding a maximum value?

I am working on a simple video game program for school and I have created a method where the player gets 15 health points if that method is called. I have to keep the health at a max of 100 and with my limited programming ability at this point I am…
user2053184