m_v*_*aly 18 c java language-agnostic programming-languages switch-statement
语法太多,语法相同:
switch (someValue) {
case OPTION_ONE:
case OPTION_LIKE_ONE:
case OPTION_ONE_SIMILAR:
doSomeStuff1();
break; // EXIT the switch
case OPTION_TWO_WITH_PRE_ACTION:
doPreActionStuff2();
// the default is to CONTINUE to next case
case OPTION_TWO:
doSomeStuff2();
break; // EXIT the switch
case OPTION_THREE:
doSomeStuff3();
break; // EXIT the switch
}
Run Code Online (Sandbox Code Playgroud)
现在所有人都知道break语句是必需的,因为当语句丢失时,语句switch将继续到下一个语句.我们有一个例子,和.问题是我们只需要非常非常非常需要"跳到下一个案例".而且我们经常在结束时放下休息时间.casebreakOPTION_LIKE_ONEOPTION_ONE_SIMILAROPTION_TWO_WITH_PRE_ACTIONcase
初学者很容易忘记它.我的一位C老师甚至向我们解释过,好像这是C语言中的一个错误(不想谈论:)
我想问一下,我是否还有其他任何语言,我不知道(或忘记)处理这样的开关/案例:
switch (someValue) {
case OPTION_ONE: continue; // CONTINUE to next case
case OPTION_LIKE_ONE: continue; // CONTINUE to next case
case OPTION_ONE_SIMILAR:
doSomeStuff1();
// the default is to EXIT the switch
case OPTION_TWO_WITH_PRE_ACTION:
doPreActionStuff2();
continue; // CONTINUE to next case
case OPTION_TWO:
doSomeStuff2();
// the default is to EXIT the switch
case OPTION_THREE:
doSomeStuff3();
// the default is to EXIT the switch
}
Run Code Online (Sandbox Code Playgroud)
第二个问题:为什么C中有这样的历史意义?可能会继续使用下一个案例的次数远远超过我们现在使用的情况吗?
我认为Scala模式匹配在这些情况下是一个巨大的改进.:)
object MatchTest2 extends Application {
def matchTest(x: Any): Any = x match {
case 1 => "one"
case "two" => 2
case y: Int => "scala.Int"
}
println(matchTest("two"))
}
Run Code Online (Sandbox Code Playgroud)
来自scala-lang.org的样本
而VB .NET更像是你期望它应该如何工作.
Select Case i
Case 1 to 3
DoStuff(i)
Case 4,5,6
DoStuffDifferently(i)
Case Is >= 7
DoStuffDifferentlyRedux(i)
Case Else
DoStuffNegativeNumberOrZero(i)
End Select
Run Code Online (Sandbox Code Playgroud)
没有可能使用Goto,没有任何堕落