【发布时间】:2013-03-05 01:45:36
【问题描述】:
switch(1){
case 1: print 1; // prints 1 (as expected)
case 2: print 2; // prints 2 (even though match is not equal?)
case 3: print 3; // prints 3 (even though match is not equal?)
}
我知道,如果您在每个 case 表达式匹配后不使用 break,大多数编程语言都会继续执行每个语句。但是我很困惑为什么大多数语言都会在第二个和第三个 case 语句上执行一个 case 块作为成功匹配。
只是为了澄清: 我知道 switch 语句的行为,但我不明白即使没有找到匹配项,将 case 块/语句作为成功匹配执行也是有意义的逻辑。
更新:我刚刚更新了问题以反映大多数编程语言,而不仅仅是 PHP。
【问题讨论】:
-
请注意,switch 是一个可以多种方式使用的语句。它不仅仅是许多其他 if 的替代品。
标签: php javascript c switch-statement case