switch语句中的fallthrough行为

huangapple go评论81阅读模式
英文:

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块。

现在我有两个问题:

  1. 为什么Go默认进行比较,而不是只执行与k相等的case?
  2. 文中提到后面的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 &quot;fmt&quot;

 func main() {
 k := 6
 switch k {
    case 4: fmt.Println(&quot;was &lt;= 4&quot;); fallthrough;
    case 5: fmt.Println(&quot;was &lt;= 5&quot;); fallthrough;
    case 6: fmt.Println(&quot;was &lt;= 6&quot;); fallthrough;
    case 7: fmt.Println(&quot;was &lt;= 7&quot;); fallthrough;
    case 8: fmt.Println(&quot;was &lt;= 8&quot;); fallthrough;
    default: fmt.Println(&quot;default case&quot;) 
    }
}

The output is:

was &lt;= 6
was &lt;= 7
was &lt;= 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:

  1. Why does Go compare by default, in which case k is lower?
  2. 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 4case 5的结果为falsecase 6的结果为truecase 7case 8default的结果也为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.

huangapple
  • 本文由 发表于 2015年6月27日 23:33:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/31090405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定