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
115
votes
9 answers

Switch case in C# - a constant value is expected

My code is as follows: public static void Output(IEnumerable dataSource) where T : class { dataSourceName = (typeof(T).Name); switch (dataSourceName) { case (string)typeof(CustomerDetails).Name.ToString(); : …
johnnie
  • 1,837
  • 5
  • 23
  • 36
114
votes
4 answers

Swift: testing against optional value in switch case

In Swift, how can I write a case in a switch statement that tests the value being switched against the contents of an optional, skipping over the case if the optional contains nil? Here's how I imagine this might look: let someValue = 5 let…
George WS
  • 3,903
  • 5
  • 27
  • 39
113
votes
18 answers

Switch statement with returns -- code correctness

Let's say I have code in C with approximately this structure: switch (something) { case 0: return "blah"; break; case 1: case 4: return "foo"; break; case 2: case 3: return "bar"; break; …
houbysoft
  • 32,532
  • 24
  • 103
  • 156
113
votes
12 answers

How to make C# Switch Statement use IgnoreCase

If I have a switch-case statement where the object in the switch is string, is it possible to do an ignoreCase compare? I have for instance: string s = "house"; switch (s) { case "houSe": s = "window"; } Will s get the value "window"? How do I…
Tolsan
  • 1,292
  • 2
  • 9
  • 12
112
votes
10 answers

When to use If-else if-else over switch statements and vice versa

Why you would want to use a switch block over a series of if statements? switch statements seem to do the same thing but take longer to type.
K2J
  • 2,573
  • 6
  • 27
  • 34
112
votes
20 answers

Using switch statement with a range of value in each case?

In Java, is it possible to write a switch statement where each case contains more than one value? For example (though clearly the following code won't work): switch (num) { case 1 .. 5: System.out.println("testing case 1 to 5"); …
davidx1
  • 3,525
  • 9
  • 38
  • 65
111
votes
13 answers

How to shorten switch case block converting a number to a month name?

Is there a way to write this on fewer lines, but still easily readable? var month = ''; switch(mm) { case '1': month = 'January'; break; case '2': month = 'February'; break; case '3': month =…
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
111
votes
6 answers

Declaring and initializing variables within Java switches

I have a crazy question about Java switches. int key = 2; switch (key) { case 1: int value = 1; break; case 2: value = 2; System.out.println(value); break; default: break; } Scenario 1 -…
ironwood
  • 8,936
  • 15
  • 65
  • 114
110
votes
11 answers

C# how to use enum with switch

I can't figure out how to use switches in combination with an enum. Could you please tell me what I'm doing wrong, and how to fix it? I have to use an enum to make a basic calculator. public enum Operator { PLUS, MINUS, MULTIPLY,…
yesman
  • 7,165
  • 15
  • 52
  • 117
109
votes
4 answers

switch() statement usage

I am a little confused about the switch statement in R. Simply googling the function I get an example as follows: A common use of switch is to branch according to the character value of one of the arguments to a function. > centre <- function(x,…
LostLin
  • 7,762
  • 12
  • 51
  • 73
109
votes
16 answers

How can I compare strings in C using a `switch` statement?

In C there is a switch construct which enables one to execute different conditional branches of code based on an test integer value, e.g., int a; /* Read the value of "a" from some source, e.g. user input */ switch (a) { case 100: // Code …
Niklas
  • 1,177
  • 2
  • 8
  • 6
106
votes
5 answers

Switch case on type c#

Possible Duplicate: C# - Is there a better alternative than this to 'switch on type'? Hello suppose i get a big if/else on class type. it's there a way to do it with a switch case ? Example : function test(object obj) { if(obj is…
Cédric Boivin
  • 10,854
  • 13
  • 57
  • 98
106
votes
4 answers

This source code is switching on a string in C. How does it do that?

I'm reading through some emulator code and I've countered something truly odd: switch (reg){ case 'eax': /* and so on*/ } How is this possible? I thought you could only switch on integral types. Is there some macro trickery going on?
Ian Colton
  • 865
  • 2
  • 6
  • 7
106
votes
7 answers

Variable's scope in a switch case

I think I don't understand how the scope works in a switch case. Can someone explain to me why the first code doesn't compile but the second does ? Code 1 : int key = 2; switch (key) { case 1: String str = "1"; return str; case 2: …
Philippe Carriere
  • 3,712
  • 4
  • 25
  • 46
105
votes
4 answers

Why does C# have break if it's not optional?

When I create a switch statement in VS2008 C# like this (contrived): switch (state) { case '1': state = '2'; case '2': state = '1'; } it complains that I'm not allowed to drop through: Control cannot fall through from one…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953