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
11,811
questions
2304
votes
29
answers
1.1m
views
How to write a switch statement in Ruby
How do I write a switch statement in Ruby?
1717
votes
44
answers
2.3m
views
Replacements for switch statement in Python?
I want to write a function in Python that returns different fixed values based on the value of an input index.
In other languages I would use a switch or case statement, but Python does not appear ...
1207
votes
26
answers
1.2m
views
Switch statement for multiple cases in JavaScript
I need multiple cases in switch statement in JavaScript, Something like:
switch (varName)
{
case "afshin", "saeed", "larry":
alert('Hey');
break;
...
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 ...
1050
votes
14
answers
774k
views
Why can't I use switch statement on a String?
Is this functionality going to be put into a later Java version?
Can someone explain why I can't do this, as in, the technical way Java's switch statement works?
895
votes
2
answers
2.5m
views
What is the Python equivalent for a case/switch statement? [duplicate]
Is there a Python equivalent for the switch statement?
761
votes
26
answers
1.4m
views
Multiple cases in switch statement
Is there a way to fall through multiple case statements without stating case value: repeatedly?
I know this works:
switch (value)
{
case 1:
case 2:
case 3:
// Do some stuff
...
538
votes
7
answers
233k
views
When to favor ng-if vs. ng-show/ng-hide?
I understand that ng-show and ng-hide affect the class set on an element and that ng-if controls whether an element is rendered as part of the DOM.
Are there guidelines on choosing ng-if over ng-...
453
votes
17
answers
808k
views
Using two values for one switch case statement
In my code, the program does something depending on the text entered by the user. My code looks like:
switch (name) {
case text1: {
//blah
break;
}
...
427
votes
14
answers
546k
views
Is "else if" faster than "switch() case"? [duplicate]
I'm an ex Pascal guy, currently learning C#.
My question is the following:
Is the code below faster than making a switch?
int a = 5;
if (a == 1)
{
....
}
else if(a == 2)
{
....
}
else if(a =...
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[] ...
417
votes
32
answers
174k
views
Is there a better alternative than this to 'switch on type'?
Seeing as C# can't switch on a Type (which I gather wasn't added as a special case because is relationships mean that more than one distinct case might apply), is there a better way to simulate ...
409
votes
6
answers
200k
views
Case statement with multiple values in each 'when' block
The best way I can describe what I'm looking for is to show you the failed code I've tried thus far:
case car
when ['honda', 'acura'].include?(car)
# code
when 'toyota' || 'lexus'
# code
end
I've ...
405
votes
8
answers
233k
views
Java: using switch statement with enum under subclass
First I'll state that I'm much more familiar with enums in C# and it seems like enums in java is a quite mess.
As you can see, I'm trying to use a switch statement @ enums in my next example but I ...
367
votes
30
answers
431k
views
Is it possible to use the instanceof operator in a switch statement?
I have a question of using switch case for instanceof object:
For example: my problem can be reproduced in Java:
if(this instanceof A)
doA();
else if(this instanceof B)
doB();
else if(this ...