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
1,085
questions
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 ...
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?
335
votes
23
answers
601k
views
Why can't the switch statement be applied to strings?
Compiling the following code gives the error message: type illegal.
int main()
{
// Compilation error - switch expression of type illegal
switch(std::string("raj"))
{
case&...
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 ...
108
votes
16
answers
86k
views
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?
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?
274
votes
21
answers
145k
views
Is there any significant difference between using if/else and switch-case in C#?
What is the benefit/downside to using a switch statement vs. an if/else in C#. I can't imagine there being that big of a difference, other than maybe the look of your code.
Is there any reason why the ...
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-...
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 =...
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 ...
62
votes
8
answers
107k
views
Expression inside switch case statement
I'm trying to create a switch statement but I can't seem to be able to use an expression that gets evaluated (rather than a set string/integer). I can easily do this with if statements but case should ...
113
votes
21
answers
256k
views
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:
...
352
votes
10
answers
446k
views
Switch statement for greater-than/less-than
so I want to use a switch statement like this:
switch (scrollLeft) {
case (<1000):
//do stuff
break;
case (>1000 && <2000):
//do stuff
break;
}
Now I know that ...
2304
votes
29
answers
1.1m
views
How to write a switch statement in Ruby
How do I write a switch statement in Ruby?