# 05.03-switch

假設我們想要寫一個程式，印出數字的英文，我們可以用較早之前學到的方法處理：

```go
if i == 0 { 
    fmt.Println("Zero") 
} else if i == 1 {
    fmt.Println("One")
} else if i == 2 {
    fmt.Println("Two")
} else if i == 3 {
    fmt.Println("Three")
} else if i == 4 { 
    fmt.Println("Four")
} else if i == 5 {
    fmt.Println("Five")
}
```

因為用這個方式來寫程式有點乏味，所以 Go 提供另一種比較簡單的寫法：`switch` statement，我們可以像這樣來重寫程式：

```go
switch i {
case 0: fmt.Println("Zero")
case 1: fmt.Println("One")
case 2: fmt.Println("Two")
case 3: fmt.Println("Three")
case 4: fmt.Println("Four")
case 5: fmt.Println("Five")
default: fmt.Println("Unknown Number")
}
```

switch statement 以 `switch` 為開頭，並接著一個表示式（expression）【本例為 `i`】，並接著一連串的 `case`。表示式的值會與每個關鍵字 `case` 後頭的表示式比較，如果相等，則會執行 `:` 後面的 statements。

與 if statement 類似，由上而下檢查每個 case，並選擇第一個成功的 case。switch 也有提供 default（預設的）case，若在 default 之前的 cases 都沒有符合條件，則會執行這個 default case（類似 if statement 裡的 else）。

以上是主要的流程控制 statements，其它部分將在後續章節探討。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://go.netdpi.net/control/05.03-switch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
