亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C 语言 switch-case 详解:语法、实例与最佳实践

什么是 C 语言 switch 语句?

switch 结构对单个表达式求值,并根据匹配的 case 标签分派程序流程。若无匹配,执行可选的 default 块。这种决策工具特别适合处理离散常量值,如菜单选项、错误码、用户选择。

每个 case 由唯一常量表达式标识。表达式值匹配某 case 标签时,对应语句块执行,随后退出 switch(除非省略 break 导致 fall-through 穿透)。

switch-case 语法

switch (expression) {
    case constant1:
        // statements
        break;
    case constant2:
        // statements
        break;
    // …
    default:
        // statements
        break;
}
// switch 后续语句

switch 语句工作流程

下图展示 switch 语句的决策流程:

C 语言 switch-case 详解:语法、实例与最佳实践

示例 1:基础 switch

#include <stdio.h>
int main() {
    int num = 8;
    switch (num) {
        case 7:
            printf("Value is 7");
            break;
        case 8:
            printf("Value is 8");
            break;
        case 9:
            printf("Value is 9");
            break;
        default:
            printf("Out of range");
            break;
    }
    return 0;
}

输出:

Value is 8

示例 2:使用 default

#include <stdio.h>
int main() {
    int language = 10;
    switch (language) {
        case 1:
            printf("C#\n");
            break;
        case 2:
            printf("C\n");
            break;
        case 3:
            printf("C++\n");
            break;
        default:
            printf("Other programming language\n");
    }
    return 0;
}

输出:

Other programming language

示例 3:合并 case

#include <stdio.h>
int main() {
    int number = 5;
    switch (number) {
        case 1:
        case 2:
        case 3:
            printf("One, Two, or Three.\n");
            break;
        case 4:
        case 5:
        case 6:
            printf("Four, Five, or Six.\n");
            break;
        default:
            printf("Greater than Six.\n");
    }
    return 0;
}

输出:

Four, Five, or Six.

嵌套 switch

switch 可嵌套,实现分层决策。下例演示简单认证流程:

#include <stdio.h>
int main() {
    int ID, password;
    printf("Please Enter Your ID:\n");
    scanf("%d", &ID);
    switch (ID) {
        case 500:
            printf("Enter your password:\n");
            scanf("%d", &password);
            switch (password) {
                case 0:
                    printf("Welcome Dear Programmer\n");
                    break;
                default:
                    printf("Incorrect password\n");
            }
            break;
        default:
            printf("Incorrect ID\n");
    }
    return 0;
}

输出:

 Please Enter Your ID: 500
Enter your password: 000
Welcome Dear Programmer
C 语言 switch-case 详解:语法、实例与最佳实践

为何 switch 优于冗长 if-else

虽然 if-else 链能实现相同逻辑,但选项增多时极其笨重。switch 语法更清晰、可读性更强,编译器还能针对常量表达式优化为跳转表。

switch 使用规则

核心要点

C语言

  1. 开关控制电路实战:从原理图到面包板的基础电路构建
  2. 用复数演示交流电压叠加:三电源串联案例
  3. 掌握 C# switch 语句:语法、示例与最佳实践
  4. C++ do-while 循环详解:语法、实战示例与嵌套循环
  5. 掌握 C++ switch-case:语法、用法与实战示例
  6. C++ 指针精通:核心概念、代码示例与实战应用
  7. C++ 运算符重载实战指南:语法、方法、限制与代码示例
  8. C++ 函数详解:从声明到调用的实战指南
  9. C# 集合实战教程:ArrayList、Stack、Queue、Hashtable 等核心类型全解析
  10. Python print() 函数完全指南:基础用法、空行打印与自定义结束符
  11. Python str.count() 完全指南:统计字符与子串出现次数实战
  12. Python round() 函数详解与实战示例