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
78
votes
3 answers

Switch multiple case statement

Can someone suggest me how can I replace the below code? How do I rewrite the code in order to avoid the repetition of the block case 3:{code block A; break;}? switch(i) { case 1: {code block A; break;} case 2: {code block b; break;} case…
aΨVaN
  • 1,104
  • 2
  • 9
  • 18
78
votes
15 answers

Use an array as a case statement in switch

I am trying to do something like this, i.e., use an array in a switch statement. Is it possible in Java? If it isn't, please explain a possible solution. boolean[] values = new boolean[4]; values[0] = true; values[1] = false; values[2] =…
Todor Grudev
  • 1,513
  • 2
  • 14
  • 25
78
votes
13 answers

Evaluate Expressions in Switch Statements in C#

I have to implement the following in a switch statement: switch(num) { case 4: // some code ; break; case 3: // some code ; break; case 0: // some code ; break; case < 0: // some code ; break; } Is it…
priyanka.sarkar
  • 25,766
  • 43
  • 127
  • 173
77
votes
18 answers

Best way to do a PHP switch with multiple values per case?

How would you do this PHP switch statement? Also note that these are much smaller versions, the one I need to create will have a lot more values added to it. Version 1: switch ($p) { case 'home': case '': $current_home =…
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
76
votes
2 answers

What do the curly braces do in switch statement after case in es6?

What's the difference between: switch (expression) { case: somethings; break; } and switch (expression) { case: { somethings; break; } } At first I thought that I could return an object literal like so, but it…
Aurimas
  • 2,577
  • 5
  • 26
  • 37
73
votes
11 answers

Regarding Java switch statements - using return and omitting breaks in each case

Given this method, does this represent some egregious stylistic or semantic faux pas: private double translateSlider(int sliderVal) { switch (sliderVal) { case 0: return 1.0; case 1: return .9; …
NickAbbey
  • 1,201
  • 2
  • 10
  • 17
72
votes
6 answers

How to use values stored in variables as case patterns?

I'm trying to understand the new structural pattern matching syntax in Python 3.10. I understand that it is possible to match on literal values like this: def handle(retcode): match retcode: case 200: print('success') …
72
votes
12 answers

Kotlin 'when' statement vs Java 'switch'

Pattern matching in Kotlin is nice and the fact it does not execute the next pattern match is good in 90% of use cases. In Android, when database is updated, we use Java switch property to go on next case if we do not put a break to have code…
Geob-o-matic
  • 5,940
  • 4
  • 35
  • 41
72
votes
2 answers

Getting a bunch of crosses initialization error

I have this snippets of code taken from a downloaded example: bool ChatServer::event(QEvent * event) { if(event->type() == QEvent::User) { UserEvent * ue = static_cast(event); switch(ue->userType) { …
SIFE
  • 5,567
  • 7
  • 32
  • 46
71
votes
6 answers

GCC 7, -Wimplicit-fallthrough warnings, and portable way to clear them?

We are catching warnings from GCC 7 for implicit fall through in a switch statement. Previously, we cleared them under Clang (that's the reason for the comment seen below): g++ -DNDEBUG -g2 -O3 -std=c++17 -Wall -Wextra -fPIC -c authenc.cpp asn.cpp:…
jww
  • 97,681
  • 90
  • 411
  • 885
70
votes
7 answers

Case vs If Else If: Which is more efficient?

Possible Duplicates: is “else if” faster than “switch() case” ? What is the relative performance of if/else vs. switch in Java? Ive been coding-in-the-run again....when the debugger steps through a case statement it jumps to the item that matches…
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
69
votes
4 answers

How will a C# switch statement's default label handle a nullable enum?

How will a C# switch statement's default label handle a nullable enum? Will the default label catch nulls and any unhandled cases?
Trey Mack
  • 4,215
  • 2
  • 25
  • 31
68
votes
5 answers

JavaScript switch with logical operators?

for (var count = 1; count < 6; count++) { switch (count) { case (2): document.write("hi"); break; case (count > 3): document.write("bye"); break; case (count >= 4): document.write("lol"); break; …
Strawberry
  • 66,024
  • 56
  • 149
  • 197
67
votes
7 answers

Why doesn't Python have switch-case? (Update: match-case syntax was added to Python 3.10 in 2021)

Please explain why Python does not have the switch-case feature implemented in it.
T1412
  • 705
  • 1
  • 5
  • 12
67
votes
15 answers

Combine return and switch

How can I combine return and switch case statements? I want something like return switch(a) { case 1:"lalala" case 2:"blalbla" case 3:"lolollo" default:"default" }; I know about this solution…
Neir0
  • 12,849
  • 28
  • 83
  • 139