switch语句的两种写法

在编程语言中,switch 语句通常有两种主要写法,具体形式可能因语言而异。以下以常见的语言(如 C、Java、JavaScript 等)为例,说明 switch 语句的两种写法。


1. 经典写法

这种写法是传统的形式,通过 casebreak 来控制流程。

示例 (以C语言为例):
#include <stdio.h>

int main() {
    int num = 2;
    switch (num) {
        case 1:
            printf("Number is 1\n");
            break;
        case 2:
            printf("Number is 2\n");
            break;
        case 3:
            printf("Number is 3\n");
            break;
        default:
            printf("Number is not 1, 2, or 3\n");
            break;
    }
    return 0;
}
特点:
  • 使用 case 语句匹配条件。
  • 通过 break 语句避免继续执行后续的 case 语句。
  • default 用来处理不匹配任何 case 的情况。

2. 简洁写法

在某些现代语言(如 JavaScript ES6+、Java、Kotlin 等),可以通过表达式或其他简化语法实现类似功能。

示例 (以 JavaScript 的 ES6 为例,用对象模拟):
const fruit = "apple";

const message = {
  apple: "This is an apple.",
  banana: "This is a banana.",
  orange: "This is an orange."
}[fruit] || "Unknown fruit.";

console.log(message);

或者 (以 Java 的 switch 表达式为例):

String fruit = "apple";
String message = switch (fruit) {
    case "apple" -> "This is an apple.";
    case "banana" -> "This is a banana.";
    case "orange" -> "This is an orange.";
    default -> "Unknown fruit.";
};
System.out.println(message);

更为完整的写法是:

String fruit = "apple";
String message = switch (fruit) {
    case "apple" -> {
        "This is an apple.";
    }
    case "banana" -> {
    "This is a banana.";
    }
    case "orange" -> {
    "This is an orange.";
    }
    default -> {
    "Unknown fruit.";
    }
};
System.out.println(message);
特点:
  • 用对象映射(JavaScript)或表达式(Java 等)来代替传统的 switch 语句。
  • 更加简洁,易于阅读。
  • 避免了 breakfall-through 问题。

两种写法的比较

特点经典写法简洁写法
适用场景流程控制复杂的情况简单条件匹配
可读性需要注意 break 和缩进更加简洁和易读
支持的语言几乎所有支持 switch 的语言支持现代语言和语法的环境

特定用途:多个 case 共享逻辑

可以让多个 case 共用同一段代码:

(以C语言为例)

#include <stdio.h>

int main() {
    int num = 2;
    switch (num) {
        case 1:
        case 2:
        case 3:
            printf("Number is between 1 and 3\n");
            break;
        default:
            printf("Number is not between 1 and 3\n");
            break;
    }
    return 0;
}
输出:

如果 num = 2,则输出:

Number is between 1 and 3

(以Java为例)

public class SelectDay{
    public static void main(string[]args){
        /*需求:键盘录入星期数,输出工作日、休息日。(1-5)工作日,(6-7)休息日。*/
        
        //分析:
        //1.键盘录入星期数
        Scanner sc =new scanner(System.in);
        System.out.println("请录入一个整数表示星期");
        int week = sc.nextInt();
        //2.利用switch语句来进行选择
        switch(week){
            case 1,2,3,4,5 -> System.out.println("工作日");
            case 6,7 -> System.out.println("休息日");
            default -> System.out.println("没有这个星期");
        }
    }
}
当然可以!你可以将 `switch` 语句替换为 `if-else` 语句。在 C 语言中,`switch` 和 `if-else` 是可以互换使用的,虽然 `switch` 更适合处理多个固定值的情况,但某些情况下(如条件判断更复杂时),使用 `if-else` 更加灵活。 --- ### ✅ 替换前(switch 语句): ```c switch (mode) { case 1: GPIO_toggleOutputOnPin(GPIO_PORT_P1, LED1_PIN); DelayMs(500); break; case 2: GPIO_toggleOutputOnPin(GPIO_PORT_P2, LED2_PIN); DelayMs(300); break; case 3: GPIO_toggleOutputOnPin(GPIO_PORT_P3, LED3_PIN); DelayMs(200); break; default: GPIO_setOutputLowOnPin(GPIO_PORT_P1, LED1_PIN); GPIO_setOutputLowOnPin(GPIO_PORT_P2, LED2_PIN); GPIO_setOutputLowOnPin(GPIO_PORT_P3, LED3_PIN); break; } ``` --- ### ✅ 替换后(if-else 语句): ```c if (mode == 1) { GPIO_toggleOutputOnPin(GPIO_PORT_P1, LED1_PIN); DelayMs(500); } else if (mode == 2) { GPIO_toggleOutputOnPin(GPIO_PORT_P2, LED2_PIN); DelayMs(300); } else if (mode == 3) { GPIO_toggleOutputOnPin(GPIO_PORT_P3, LED3_PIN); DelayMs(200); } else { GPIO_setOutputLowOnPin(GPIO_PORT_P1, LED1_PIN); GPIO_setOutputLowOnPin(GPIO_PORT_P2, LED2_PIN); GPIO_setOutputLowOnPin(GPIO_PORT_P3, LED3_PIN); } ``` --- ### ✅ 完整主循环代码片段(含 if-else): ```c while (1) { CheckButtonPress(); if (mode == 1) { GPIO_toggleOutputOnPin(GPIO_PORT_P1, LED1_PIN); DelayMs(500); } else if (mode == 2) { GPIO_toggleOutputOnPin(GPIO_PORT_P2, LED2_PIN); DelayMs(300); } else if (mode == 3) { GPIO_toggleOutputOnPin(GPIO_PORT_P3, LED3_PIN); DelayMs(200); } else { GPIO_setOutputLowOnPin(GPIO_PORT_P1, LED1_PIN); GPIO_setOutputLowOnPin(GPIO_PORT_P2, LED2_PIN); GPIO_setOutputLowOnPin(GPIO_PORT_P3, LED3_PIN); } } ``` --- ### ✅ 说明: | 语句类型 | 适用场景 | |---------|----------| | `switch` | 多个固定值判断,代码更清晰 | | `if-else` | 条件更复杂、范围判断、逻辑组合判断更灵活 | 在这个例子中,由于 `mode` 是整数且取值有限(0~3),两种写法都可以。使用 `if-else` 更加直观,特别是在嵌入式开发中,逻辑分支不多时也常使用。 --- ### ✅ 相关问题
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值