在C#中使用switch语句与使用语句的好处/缺点是什么?if/else我无法想象除了你的代码外观之外还有其他重大差异.
是否有任何理由导致生成的IL或相关的运行时性能完全不同?
使用switch语句与使用if30个unsigned枚举的语句的最佳实践是什么,其中大约10个具有预期的操作(目前是相同的操作).需要考虑性能和空间,但并不重要.我已经抽象了代码片段,所以不要因为命名惯例而讨厌我.
switch 声明:
// numError is an error enumeration type, with 0 being the non-error case
// fire_special_event() is a stub method for the shared processing
switch (numError)
{
case ERROR_01 : // intentional fall-through
case ERROR_07 : // intentional fall-through
case ERROR_0A : // intentional fall-through
case ERROR_10 : // intentional fall-through
case ERROR_15 : // intentional fall-through
case ERROR_16 : // intentional fall-through
case ERROR_20 :
{
fire_special_event();
}
break;
default:
{ …Run Code Online (Sandbox Code Playgroud) 由于编译器优化,Switch语句通常比等效的if-else-if语句更快(例如本文中描述的).
这种优化实际上如何运作?有没有人有一个很好的解释?
有三个环在C: ,for,while和do-while.他们之间有什么区别?
例如,似乎几乎所有while语句都可以被for语句替换,对吧?那么,使用的优势是什么while?
开关似乎没那么无用,因为它们可以用if-else语句替换,它可以做的不仅仅是匹配一个char/int/enum等.我只能想到一个很好用于一个开关,那将是解释命令线args.
switch语句有哪些实际用途?
我知道switch语句不可用CodeDom以及编译器如何处理switch语句.
因此,出于性能原因,当存在许多情况时,我不想使用If-else
为什么switch语句而不是if-else?
是否可以生成代码来模拟给定案例列表的Jump表.
switch(value) {
case 0: return Method0();
case 1: return Method1();
case 4; return Method4();
}
Run Code Online (Sandbox Code Playgroud)
会产生:
private delegate object Method();
Method[] _jumpTable = new Method[] { Method0, Method1, null, null, Method4 };
private object GetValue(int value)
{
if (value < 0 || value > 4)
return null;
return _jumpTable[value]();
}
Run Code Online (Sandbox Code Playgroud)
如果序列中有漏洞或列表稀疏,分析案例列表并生成数组的最佳方法是什么?
如果我想将这段代码放到一行怎么办?
if (count >= 0 && count <= 199) {
return 1;
} else if (count >= 200 && count <= 399) {
return 2;
} else if (count >= 400 && count <= 599) {
return 3;
} else if (count >= 600 && count <= 799) {
return 4;
} else {
return 5;
}
Run Code Online (Sandbox Code Playgroud)
我只想知道这几行代码是否有任何捷径.
这是我想问的代码,如果有更好的方法可以做到这一点?我将需要更多的"其他如果"选项,我担心性能
if($_GET['begin'] === 'yeg'){
} else if ($_GET['begin'] === 'deda') {
} else if ($_GET['begin'] === 'beara') {
} else if ($_GET['begin'] === 'eima') {
} else if ($_GET['begin'] === 'aba') {
}
Run Code Online (Sandbox Code Playgroud) switch语句似乎完全没用.它能做的任何事情都可以通过if和else来完成.
他们甚至可能编译成相同的代码.
那么为什么要费心呢?
break交换机中的语句label:让我发疯,这种格式让我想起了goto.
这是针对objective-c,c,C++的.我不确定vb.net是否有switch语句,但即使它确实如此,我必须忘记因为我从来没有使用它.
我的程序假设提示用户输入数字1-12并输出相应的月份.好的我知道我错过了这个项目的一个非常重要的部分,但是我知道我正在努力弄清楚要使用什么.我是否需要一个包含所有月份名称的字符串?此外,我知道我需要在cout <<"月份"之后放置一些东西.<<必须要去的地方所以答案会打印出来,但我现在还不确定.我也认为我需要有int int = something但不确定它应该是1-12还是monthname.这是我正在编辑的程序,但现在它有一个调试错误,变量"month"正在使用而没有被初始化.那是什么意思?
#include <iostream>
#include <string>
using namespace std;
char chr;
int main()
{
int month;
cout<<"Enter a number from 1-12.";
if (month ==1)
cout<<"January";
else if (month==2)
cout<< "February";
else if (month==3)
cout<<"March";
else if (month==4)
cout<<"April";
else if (month==5)
cout<<"May";
else if (month==6)
cout<<"June";
else if (month==7)
cout<<"July";
else if (month==8)
cout<<"August";
else if (month==9)
cout<<"September";
else if (month==10)
cout<<"October";
else if (month==11)
cout<<"November";
else if (month==12)
cout<<"December";
else if (month>12)
cout<<"Sorry I need …Run Code Online (Sandbox Code Playgroud) c++ ×4
if-statement ×4
c# ×3
c ×2
.net ×1
codedom ×1
for-loop ×1
loops ×1
objective-c ×1
optimization ×1
performance ×1
php ×1
while-loop ×1