> For the complete documentation index, see [llms.txt](https://labspc.gitbook.io/cnippets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://labspc.gitbook.io/cnippets/chap3.-kong-zhi-liu/3.4-switch-yu-ju.md).

# 3.4 switch 语句

记住一些编程建议 or 教导：

<figure><img src="https://labspc.com/wp-content/uploads/2024/01/1705732065-word-image-305-1.png" alt=""><figcaption></figcaption></figure>

{% code title="3.4.1 choise-your-num.c" lineNumbers="true" %}

```c
#include <stdio.h>

int main() {
    int choice;
    printf("请选择一个数字（1-3）：");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("您选择了数字 1。\n");
            break;
        case 2:
            printf("您选择了数字 2。\n");
            break;
        case 3:
            printf("您选择了数字 3。\n");
            break;
        default:
            printf("无效的选择。\n");
            break;
    }

    return 0;
}
```

{% endcode %}

```
➜  
请选择一个数字（1-3）：1
您选择了数字 1。

Saving session...completed.
```

注意：

* 各分支表达式必须互不相同
* default 分支是可选的
* 无匹配，switch 语句不执行什么动作
