切换声明是我喜爱switch与if/else if构造的个人主要原因之一.这里有一个例子:
static string NumberToWords(int number)
{
string[] numbers = new string[]
{ "", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine" };
string[] tens = new string[]
{ "", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety" };
string[] teens = new string[]
{ "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen" };
string ans = "";
switch (number.ToString().Length)
{
case 3:
ans += string.Format("{0} hundred and ", numbers[number / 100]); …Run Code Online (Sandbox Code Playgroud) 在C#中使用switch语句与使用语句的好处/缺点是什么?if/else我无法想象除了你的代码外观之外还有其他重大差异.
是否有任何理由导致生成的IL或相关的运行时性能完全不同?
我有以下开关简单案例:
let ca: string = "2";
switch (ca) {
case "2":
console.log("2");
case "1":
console.log("1");
default:
console.log("default");
Run Code Online (Sandbox Code Playgroud)
}
我试图理解为什么这段代码的输出是:
2
1
default
Run Code Online (Sandbox Code Playgroud)
我的预期输出是
2
default
为什么它的印刷品
1
即使 ca 不等于“1”?
编辑:我知道我可以添加break声明 - 我只是想了解为什么会case "1"发生,如果ca="2"
谢谢。
enum SQLErrorCode{
OK = 0,
PARTIAL_OK = 1,
SOMEWHAT_OK = 2,
NOT_OK = 3,
};
Run Code Online (Sandbox Code Playgroud)
代码1:
int error = getErrorCode();
if((error == SQLErrorCode.PARTIAL_OK) ||
(error == SQLErrorCode.SOMEWHAT_OK) ||
(error == SQLErrorCode.NOT_OK) ||
(error < 0))
callFunction1();
else
callFunction2();
Run Code Online (Sandbox Code Playgroud)
代码2:
switch(error){
case SQLErrorCode.PARTIAL_OK:
callFunction1();
break;
case SQLErrorCode.SOMEWHAT_OK:
callFunction1();
break;
case SQLErrorCode.NOT_OK:
callFunction1();
break;
default:
callFunction2();
break;
}
Run Code Online (Sandbox Code Playgroud)
我应该选择哪种方法.就性能而言,应该没有太大差异.如何在switch case中处理错误<0条件.
编辑:乔尔的解决方案:
switch(error) {
case SQLErrorCode.PARTIAL_OK:
case SQLErrorCode.SOMEWHAT_OK:
case SQLErrorCode.NOT_OK:
callFunction1();
break;
case SQLErrorCode.OK:
callFunction2();
break;
default: // error < 0 is handled …Run Code Online (Sandbox Code Playgroud) 在C++中编写switch语句时,似乎必须在每个case之后包含一个break.否则,代码将继续运行到下一个案例.
例如:
int x = 1;
switch (x)
{
case 0:
std::cout << "x is 0." << std::endl;
case 1:
std::cout << "x is 1." << std::endl;
case 2:
std::cout << "x is 2." << std::endl;
default:
std::cout << "x is neither 0, 1 nor 2." << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
将返回:
>> x is 1.
>> x is 2.
Run Code Online (Sandbox Code Playgroud)
然而:
int x = 1;
switch (x)
{
case 0:
std::cout << "x is 0." << …Run Code Online (Sandbox Code Playgroud) 我有以下代码片段.
var caseObj = function () {
}
switch (typeof caseObj) {
case "function":
console.log("it is function");
case "object":
console.log("It is object now");
}
Run Code Online (Sandbox Code Playgroud)
它的输出是
it is function.
It is object now.
Run Code Online (Sandbox Code Playgroud)
但是typeof caseObj给出输出function但它仍然评估
案例"对象"的情况.
怎么可能?我做错了吗?
编辑:
typeof caseObj给予function,所以它执行该案件,但它也执行object案件.为什么这种奇怪的行为?
进入下面的代码case 1:来case 2:和case 5:似乎有执行任何代码.我的问题是,我们不能省略输入它们吗?
switch(c)
{
case 1:
case 2:
case 3:
a++;
break;
case 5:
default:
b++;
break;
}
Run Code Online (Sandbox Code Playgroud)