英文:
Behavior of falltrough in switch statement
问题
我正在为您翻译以下内容:
我正在阅读关于Go语言中switch语句的这本书的一节。但是这个例子让我感到困惑:
package main
import "fmt"
func main() {
k := 6
switch k {
case 4:
fmt.Println("was <= 4")
fallthrough
case 5:
fmt.Println("was <= 5")
fallthrough
case 6:
fmt.Println("was <= 6")
fallthrough
case 7:
fmt.Println("was <= 7")
fallthrough
case 8:
fmt.Println("was <= 8")
fallthrough
default:
fmt.Println("default case")
}
}
输出结果为:
was <= 6
was <= 7
was <= 8
default case
书中指出:
使用
fallthrough
语句表示需要执行当前case后面的case块。
现在我有两个问题:
- 为什么Go默认进行比较,而不是只执行与k相等的case?
- 文中提到后面的case会被执行。好的,但为什么它们不仅在case与k相等时执行?
英文:
I was reading a section of this book about the switch statement in Go. But this example confused me:
<!-- language: lang-golang -->
package main
import "fmt"
func main() {
k := 6
switch k {
case 4: fmt.Println("was <= 4"); fallthrough;
case 5: fmt.Println("was <= 5"); fallthrough;
case 6: fmt.Println("was <= 6"); fallthrough;
case 7: fmt.Println("was <= 7"); fallthrough;
case 8: fmt.Println("was <= 8"); fallthrough;
default: fmt.Println("default case")
}
}
The output is:
was <= 6
was <= 7
was <= 8
default case
And the book points out:
> use the fallthrough statement to indicate that the case block following the current one has to be executed.
Now I've to questions:
- Why does Go compare by default, in which case k is lower?
- The text mentions that the following cases are executed. Fine. But why aren't they only executed of the case matches k?
答案1
得分: 2
《Go编程语言规范》
开关语句
在表达式开关语句中,开关表达式会被求值,而不需要是常量,而case表达式会从左到右、从上到下进行求值;第一个与开关表达式相等的case会触发执行与其关联的语句;其他的case会被跳过。如果没有任何一个case匹配,并且存在"default" case,则会执行"default" case中的语句。一个开关语句中最多只能有一个"default" case,并且可以出现在任何位置。如果缺少开关表达式,则等价于布尔值true。
ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
ExprCaseClause = ExprSwitchCase ":" StatementList .
ExprSwitchCase = "case" ExpressionList | "default" .
在case或default子句中,最后一个非空语句可以是一个(可能带标签的)"fallthrough"语句,以指示控制流从该子句的末尾流向下一个子句的第一个语句。否则,控制流会流向开关语句的末尾。"fallthrough"语句可以出现在表达式开关的除最后一个子句外的所有子句的最后一个语句。
在这个例子中,表达式的求值从上到下进行。case 4
和case 5
的结果为false
,case 6
的结果为true
。case 7
、case 8
和default
的结果也为false
,但通过使用fallthrough
流向下一个子句来执行。
英文:
> The Go Programming Language Specification
>
> Switch statements
>
> Expression switches
>
> In an expression switch, the switch expression is evaluated and the
> case expressions, which need not be constants, are evaluated
> left-to-right and top-to-bottom; the first one that equals the switch
> expression triggers execution of the statements of the associated
> case; the other cases are skipped. If no case matches and there is a
> "default" case, its statements are executed. There can be at most one
> default case and it may appear anywhere in the "switch" statement. A
> missing switch expression is equivalent to the boolean value true.
>
> ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
> ExprCaseClause = ExprSwitchCase ":" StatementList .
> ExprSwitchCase = "case" ExpressionList | "default" .
>
> In a case or default clause, the last non-empty statement may be a
> (possibly labeled) "fallthrough" statement to indicate that control
> should flow from the end of this clause to the first statement of the
> next clause. Otherwise control flows to the end of the "switch"
> statement. A "fallthrough" statement may appear as the last statement
> of all but the last clause of an expression switch.
>
> The expression may be preceded by a simple statement, which executes
> before the expression is evaluated.
Evaluate case expressions top-to-bottom. case 4
and case 5
are false
, case 6
is true
. case 7
, case 8
, and default
are false
but execute by flowing to the next clause via fallthrough
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论