为什么是以下代码:
class swi
{
public static void main(String[] args)
{
int a=98;
switch(a)
{
default:{ System.out.println("default");continue;}
case 'b':{ System.out.println(a); continue;}
case 'a':{ System.out.println(a);}
}
System.out.println("Switch Completed");
}
}
Run Code Online (Sandbox Code Playgroud)
给出错误:
继续循环
我知道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/else语句.我知道Java从Java 1.7开始支持交换机块中的字符串,但我仍在使用Java 1.6:
} else if (typeName.equals("Boolean")) {
return new SwitchInputType<Boolean>(new Boolean((String) memberValue));
} else if (typeName.equals("Double")) {
return new SwitchInputType<Double>(new Double((String) memberValue));
} else if (typeName.equals("Int32"))
Run Code Online (Sandbox Code Playgroud) 在R中,有没有办法制作一个switch语句,以便为两种不同的情况执行相同的代码块?显然我可以复制并粘贴两个语句的整个代码,但我希望有一个更简洁的方法来做到这一点.
我也可以使用if-else块来避免重复大块代码,但是在R中切换通常更快.
由于R将switch语句解析为函数的方式似乎不太可能,但我希望R的开发人员特别注意解析switch语句以允许多个参数引用相同的代码块.
本质上,我有一个伽马探测器系统,每个探测器被分成4个晶体,如果只有2个晶体发生碰撞,我们可以确定该对晶体是垂直于还是平行于产生伽马射线的反应平面。在为此编写逻辑的过程中,我最后编写了一个庞大而丑陋的switch语句组合,这些语句在每个检测器中检查晶体编号的组合(在整个检测器及其晶体阵列中都是唯一的)。这是代码,包括相关功能。
//The Parallel and Perpendicular designations are used in addition to the Double
//designation for the 90 degree detectors if we get a diagonal scatter in those detectors
//then we use the Double designation
enum ScatterType{Single, Double, Triple, Quadruple, Parallel, Perpendicular};
ScatterType EventBuffer::checkDoubleGamma(int det)
{
int num1=evList[crysList[0]].crystalNum;
int num2=evList[crysList[1]].crystalNum;
switch(det)
{
case 10: //first of the 90 degree detectors
if( (num1==40 && num2==41) || //combo 1
(num1==41 && num2==40) || //combo 1 reverse
(num1==42 && num2==43) || //combo 2
(num1==43 …Run Code Online (Sandbox Code Playgroud) 我正在搜索if-else vs三元运算符vs switch case,但找不到所有这三个比较的帖子.但是,我发现了一些好帖子,发现switch-case比if-else更快.你可以查看下面的一个:
然后我遇到一些帖子,说if-else和三元运算符之间没有性能差异.其中一个最相关的帖子如下:
但是,我没有找到任何关于switch-case与三元运营商的相关帖子.
所以,我只想知道我是否可以得出结论,switch case比三元运算符和if-else更快?
我知道这是一个愚蠢的问题,但我想知道答案.
java performance if-statement ternary-operator switch-statement
我正在阅读有关C编程的本教程.它说:
switch语句实际上完全不同(来自其他语言),实际上是一个"跳转表".您可以只放置导致整数的表达式,而不是随机布尔表达式,这些整数用于计算从开关顶部到匹配该值的部分的跳转.这里有一些代码我们将分解以理解"跳转表"的概念.
但是,需要比较switch语句的情况,直到找到匹配项(否则返回default).
它与多个if-else语句有什么不同呢?或者,它只是一个语法糖?我错过了一些重要的东西吗?
我有这个x86汇编代码,我正在尝试将其转换为C:
.GLOBAL calculate
calculate:
pushl %ebp
movl %esp,%ebp
movl 12(%ebp),%eax
movl 8(%ebp),%ecx
cmpl $2,%ecx
ja done
jmp *operations(,%ecx,4)
operation1:
imull %eax,%eax
jmp done
operation2:
negl %eax
jmp done
operation3:
addl $0x80,%eax
done:
leave
ret
operations:
.long operation1, operation2, operation3
Run Code Online (Sandbox Code Playgroud)
我的问题是关于这jmp *operations(,%ecs,4)条线.我认为这是一个switch语句,我知道它在内存中是如何工作的,但是它如何转换为C?我不是必须知道这些位置的堆栈上有什么才能为它写一个开关吗?
这就是我所拥有的:
int calculate(int a, int b)
{
if (2 > a)
{
return b;
}
switch(a) {
case /* ? */:
b = (b * b);
break;
case /* ? */:
b = (b * -1);
break; …Run Code Online (Sandbox Code Playgroud) 我刚发现这个
foo = case ((), ()) of
( ()
, () ) -> ()
Run Code Online (Sandbox Code Playgroud)
失败了
/tmp/wtmpf-file11080.hs:3:8:
parse error (possibly incorrect indentation or mismatched brackets)
Run Code Online (Sandbox Code Playgroud)
这可以通过缩进模式的第二行来实现
foo = case ((), ()) of
( ()
, () ) -> ()
Run Code Online (Sandbox Code Playgroud)
但这感觉与我平常的风格不一致,特别是在
bar = case ( some lengthy :: Complicated typed expression
, another also lengthy :: Expression with (Other types) ) of
( Complicated (Pattern match) to (unwrap)
, Expression that's (Again not so short) ) -> the Rest of my Code …Run Code Online (Sandbox Code Playgroud) 我想通过匹配数组类型来使用switch语句.我有以下课程.
class A {}
class B : A {}
Run Code Online (Sandbox Code Playgroud)
let a : A = B()
switch a {
case let b as B:
print("b = \(b)")
default:
print("unknown type")
}
Run Code Online (Sandbox Code Playgroud)
let aArray : [A] = [B(), B()]
switch aArray {
case let bArray as [B] :
print("bArray = \(bArray)")
default:
print("unknown type")
}
Run Code Online (Sandbox Code Playgroud)
Downcast pattern value of type '[B]' cannot be used
Run Code Online (Sandbox Code Playgroud)
注意:在Swift 4上测试过
switch-statement ×10
java ×4
if-statement ×3
c ×2
c++ ×2
assembly ×1
c# ×1
codedom ×1
continue ×1
haskell ×1
indentation ×1
jump-table ×1
performance ×1
r ×1
swift ×1
syntax ×1
x86 ×1