Skip to main content

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.

switch-statement
30 votes
12 answers
24k views

using the 'is' keyword in a switch in c#

I'm currently adding some new extended classes to this code: foreach (BaseType b in CollectionOfExtendedTypes) { if (b is ExtendedType1) { ((ExtendedType1) b).foo = this; } else if (b is ...
sre's user avatar
  • 679
155 votes
7 answers
79k views

Variable declaration in a C# switch statement [duplicate]

Why is it that in a C# switch statement, for a variable used in multiple cases, you only declare it in the first case? For example, the following throws the error "A local variable named 'variable' ...
Jeremy's user avatar
  • 9,153
264 votes
12 answers
332k views

How to use a switch case 'or' in PHP

Is there a way of using an 'OR' operator or equivalent in a PHP switch? For example, something like this: switch ($value) { case 1 || 2: echo 'the value is either 1 or 2'; break;...
user avatar
180 votes
14 answers
782k views

SQL Switch/Case in 'where' clause

I tried searching around, but I couldn't find anything that would help me out. I'm trying to do this in SQL: declare @locationType varchar(50); declare @locationID int; SELECT column1, column2 FROM ...
Miles's user avatar
  • 5,726
22 votes
4 answers
32k views

Good way to do a "switch" in a Makefile

I'm experimenting with an updated build system at work; currently, I'm trying to find a good way to set compiler & flags depending on the target platform. What I would like to do is something ...
Christoffer's user avatar
  • 12.8k
14 votes
3 answers
54k views

switch case vs if else [duplicate]

I was wondering if there was any difference in the way the following code was compiled into assembly. I've heard that switch-case is more efficient than if else, but in this example I am not quite ...
Jose Vega's user avatar
  • 10.2k
179 votes
12 answers
245k views

Switch statement fall-through...should it be allowed? [closed]

For as long as I can remember I have avoided using switch statement fall-through. Actually, I can't remember it ever entering my consciousness as a possible way to do things as it was drilled into my ...
Fostah's user avatar
  • 11.6k
3 votes
7 answers
871 views

Tree Algorithm

I was thinking earlier today about an idea for a small game and stumbled upon how to implement it. The idea is that the player can make a series of moves that cause a little effect, but if done in a ...
Leahn Novash's user avatar
  • 2,911
423 votes
14 answers
238k views

Switch statement fallthrough in C#?

Switch statement fallthrough is one of my personal major reasons for loving switch vs. if/else if constructs. An example is in order here: static string NumberToWords(int number) { string[] ...
Matthew Scharley's user avatar
12 votes
5 answers
7k views

Eclipse for IntelliJ Idea Users [closed]

I have a coworker who is looking to switch from InteilliJ Idea to Eclipse, and is concerned about not knowing the Eclipse set of commands. I was wondering - would anyone have a link to keyboard ...
awied's user avatar
  • 2,698
160 votes
10 answers
27k views

Is there any benefit to this switch / pattern matching idea?

I've been looking at F# recently, and while I'm not likely to leap the fence any time soon, it definitely highlights some areas where C# (or library support) could make life easier. In particular, I'...
Marc Gravell's user avatar
2 votes
1 answer
2k views

error when switching to different svn branch

I've got two SVN branches (eg development and stable) and want to switch from one to another... In every tutorial there is command like: rootOfLocalSvnCopy:>svn switch urlToNewBranch . But it ...
Juraj's user avatar
  • 2,021
14 votes
14 answers
31k views

Objective-C switch using objects?

I'm doing some Objective-C programming that involves parsing an NSXmlDocument and populating an objects properties from the result. First version looked like this: if([elementName compare:@"...
craigb's user avatar
  • 16.9k
190 votes
23 answers
148k views

Advantage of switch over if-else statement

What's the best practice for using a switch statement vs using an if statement for 30 unsigned enumerations where about 10 have an expected action (that presently is the same action). Performance and ...
Zing-'s user avatar
  • 2,147
1136 votes
23 answers
400k views

Why can't variables be declared in a switch statement?

I've always wondered this - why can't you declare variables after a case label in a switch statement? In C++ you can declare variables pretty much anywhere (and declaring them close to first use is ...
Rob's user avatar
  • 77.9k

15 30 50 per page