作为我在Haskell中编写的迷你解释器的一部分,我正在编写一个执行以下操作的函数:如果是eval (App e1 e2),我想递归计算e1(eval e1),将结果设置为v1.然后使用Switch/Case,我想检查模式,v1如果它不是Error,则递归计算e2(eval e2)并将该值设置为v2.使用这两个值v1和v2,然后我申请的另一个功能(appVals这些值).
eval :: Exp -> Error Val
eval (App e1 e2) = appVals v1 v2 where
v1 = case (eval e1) of
Error err -> Error "Not an application"
/= Error err -> eval e1 = v1
v2 = case (eval e2) of
Error err -> Error "Not an application"
/= Error err …Run Code Online (Sandbox Code Playgroud) 以下是我需要优化和计划的代码,以便切换到最佳状态.但我可以比较一下.所以我计划将comaparision(len> 3)作为默认情况.
如果我将比较部分(len> 3)作为默认情况并在swith中添加默认值,它会更快吗?
或者我如何将下面的代码作为switch语句?
if ( len > 3 ) {
which will happen more often;
}
else if ( len == 3 ) {
next case which may occur often;
} else if ( len == 2 ) {
the next priority case;
} else {
and this case occurs rarely;
}
Run Code Online (Sandbox Code Playgroud) 是否可以在C#中添加一个额外的条件到Switch语句,如下所示
switch(MyEnum)
{
case 1:
case 2:
case 3 && Year > 2012://Additional Condtion Here
//Do Something here..........
break;
case 4:
case 5:
//Do Something here..........
break;
}
Run Code Online (Sandbox Code Playgroud)
在上面提到的例子中,如果MyEnum = 3,那么如果年> 2012,它必须被执行...是否可能?
[EDITED]
年份> 2012不适用于案例1和案例2.
private enum CLASS {FMAN, SOPH, JUN, SEN, GRAD, ERROR};
private CLASS stringToClass(String t){
switch(CLASS.valueOf(t))
{
case FMAN: return CLASS.FMAN;
case SOPH: return CLASS.SOPH;
case JUN: return CLASS.JUN;
case SEN: return CLASS.SEN;
case GRAD: return CLASS.GRAD;
default:
System.out.println("NOT VALID CLASS. ERROR");
return CLASS.ERROR;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我在这里有这段代码.在构造时,我有这个方法从给定的字符串t返回相应的值.
我的问题是,让我们说"t"最终成为"CSE".我该如何处理并基本上返回CLASS.ERROR的值.
这是在Java 6中,而不是7.
使用它的优点和缺点是什么:
String a = new String();
switch (i) {
case 1: a = "Cueck"; break;
case 2: a = "Blub"; break;
case 3: a = "Writing cases is BORING!"; break;
}
System.out.println(a);
Run Code Online (Sandbox Code Playgroud)
与:
switch (i) {
case 1: System.out.println("Cueck"); break;
case 2: System.out.println("Blub"); break;
case 3: System.out.println("Writing cases is BORING!"); break;
}
Run Code Online (Sandbox Code Playgroud)
哪个生成更好的字节码?哪个生成更多字节码?
我有一个与此类似的程序:
switch(x)
{ case 1:
for(i=0; i<10; i++)
{
if ((i==3) || (i==4) || (i==5) || (i==6) || (i==7))
{
if (foobar[i])
break; // i am talking about this break
}
}
...further program
break; /not this break
Run Code Online (Sandbox Code Playgroud)
如果foobar[i]是真的,程序是否会突破case标签或for循环?
在javascript switch语句中,如果其中任何一个case满意,我想执行一些函数:
switch (myVar){
case 0:
do_something_0();
break;
case 1:
do_something_1();
break;
// I want to execute myFunc() if myVar === 1 or myVar === 2
}
Run Code Online (Sandbox Code Playgroud)
我提出了拥有辅助变量的想法haveMatched,就像这样.
var haveMatched=false;
switch (myVar){
case 0:
do_something_0();
haveMatched=true;
break;
case 1:
do_something_1();
haveMatched=true;
break;
}
if (haveMatched){
do_finally();
}
Run Code Online (Sandbox Code Playgroud)
我认为可能有更好的方法来实现这一点(例如,如果我不知道default:关键字,我会尝试类似的方式).我做得对,还是我错过了什么?
我在intellij中使用java 1.7并且它给了我一个编译错误,好像我使用的是1.7之前的版本.说,"不兼容的类型.发现'java.lang.String'.必需'byte,char,short或int'.
我正在喋喋不休地想弄清楚为什么会这样.谢谢
我想使用以下表达式
-(void)SwitchCondn{
int expression;
int match1=0;
int match2=1;
switch (expression)
{
case match1:
//statements
break;
case match2:
//statements
break;
default:
// statements
break;
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了

当我研究我发现
In order to work in Objective-C, you should define your constant either like this:
#define TXT_NAME 1
Or even better, like this:
enum {TXT_NAME = 1};
Run Code Online (Sandbox Code Playgroud)
我很久以来一直在使用这种方法.现在我的变量值将在运行时改变,所以我需要以其他方式定义,我不想使用if else所以有任何方式的声明变量其他方式
我接受了以下研究
为什么我不能在Objective-C中的switch-case语句中使用我的常量?[error = Expression不是整数常量表达式]
对于简单的情况,if let或者guard我没有看到优势,
if case let x? = someOptional where ... {
...
}
//I don't see the advantage over the original if let
if let x = someOptional where ... {
...
}
Run Code Online (Sandbox Code Playgroud)
对于for-case-let简化使用可选集合的情况,我真的希望swift可以更进一步:
for case let x? in optionalArray {
...
}
//Wouldn't it be better if we could just write
for let x? in optionalArray {
...
}
Run Code Online (Sandbox Code Playgroud)
谷歌一段时间之后我认为唯一有用的案例是" Swift 2模式匹配:解开多个选项 ":
switch (username, password) {
case let (username?, password?):
print("Success!")
case …Run Code Online (Sandbox Code Playgroud) switch-statement ×10
java ×3
c ×2
break ×1
bytecode ×1
c# ×1
c++ ×1
enums ×1
haskell ×1
if-statement ×1
ios ×1
javascript ×1
objective-c ×1
optional ×1
recursion ×1
string ×1
subroutine ×1
swift ×1
swift2 ×1
variables ×1