在 C++ 中使用 switch 语句

Jinku Hu 2023年1月30日 2021年1月4日
  1. 使用 switch 语句构建多代码路径方案
  2. 使用 default 关键字在 switch 中指定一个默认代码路径
  3. 在不同的代码路径中使用 {} 块符号来声明局部变量
在 C++ 中使用 switch 语句

本文将介绍几种在 C++ 中使用 switch 语句的方法。

使用 switch 语句构建多代码路径方案

switch 关键字的使用与其他基于块的语句类似,这些语句将一个给定的表达式作为条件进行评估。唯一不同的是,与 case 关键字指定的每个代码路径相比,表达式必须是一个整数。路径具有以下结构-case label:statement,其中 label 是与 switch 条件中的表达式相比的值。如果表达式与其中一个标签相匹配,程序执行就会移动到该标签之后的语句。注意,如果 case 块没有以 break; 语句结束,则程序将继续执行后面所有 case 块中的语句,直到没有达到 break;switch 作用域本身结束。

#include <iostream>

using std::cout; using std::endl;
using std::cin; using std::string;

enum WEEK { Monday, Tuesday, Wednesday, Thursday,
            Friday, Saturday, Sunday};

int main() {
    int week_day;

    cout << "Type the weekday (0-6): ";
    cin >> week_day;
    if (cin.fail()) {
        cout << "Error occured when reading from stream!" << endl;
        exit(EXIT_FAILURE);
    };

    switch (week_day) {
        case Monday: cout << "Monday\n"; break;
        case Tuesday: cout << "Tuesday\n"; break;
        case Wednesday: cout << "Wednesday\n"; break;
        case Thursday: cout << "Thursday\n"; break;
        case Friday: cout << "Friday\n"; break;
        case Saturday: cout << "Saturday\n"; break;
        case Sunday: cout << "Sunday\n"; break;
        default: exit(EXIT_FAILURE);
    }

    exit(EXIT_SUCCESS);
}

使用 default 关键字在 switch 中指定一个默认代码路径

default 关键字定义了特殊情况下的标签,如果没有一个声明的情况与 switch 表达式相匹配,就会被执行。需要注意的重要一点是,每个标签块都需要在结尾处使用 break; 语句来满足 switch 结构的基本逻辑。请注意,接下来的代码示例演示了没有任何 breakswitch 语句,这将表现得相当突兀。如果 week_day 值与第一个标签 Monday 相匹配,那么每个 cout 语句都会被执行,包括与默认标签并列的语句。另一方面,如果 week_day 值不匹配任何 case 标签,那么从 default 标签开始的 cout 语句将被执行。

#include <iostream>

using std::cout; using std::endl;
using std::cin; using std::string;

enum WEEK { Monday, Tuesday, Wednesday, Thursday,
            Friday, Saturday, Sunday};

int main() {
    int week_day;

    cout << "Type the weekday (0-6): ";
    cin >> week_day;
    if (cin.fail()) {
        cout << "Error occured when reading from stream!" << endl;
        exit(EXIT_FAILURE);
    };

    switch (week_day) {
        case Monday: cout << "Monday\n";
        case Tuesday: cout << "Tuesday\n";
        case Wednesday: cout << "Wednesday\n";
        case Thursday: cout << "Thursday\n";
        case Friday: cout << "Friday\n";
        default: cout << "Wrong number specified!\n";
        case Saturday: cout << "Saturday\n";
        case Sunday: cout << "Sunday\n";
    }

    exit(EXIT_SUCCESS);
}

在不同的代码路径中使用 {} 块符号来声明局部变量

和其他基于块的语句一样,switch 体有可以声明变量的作用域,但 case 要求用户在每个 case 构造中都有局部变量。请注意,每个 case 路径的语句都有共享的作用域,除非在 {} 内的独立块内声明。因此,如果一个变量在一个 case 中被初始化,那么其他情况下的语句就不能引用同一个变量。下面的例子实现了 switch 语句,其中 case Friday 有一个本地块,而且里面的变量不能从其他 case 看到。

#include <iostream>

using std::cout; using std::endl;
using std::cin; using std::string;

enum WEEK { Monday, Tuesday, Wednesday, Thursday,
            Friday, Saturday, Sunday};

int main() {
    int week_day;

    cout << "Type the weekday (0-6): ";
    cin >> week_day;
    if (cin.fail()) {
        cout << "Error occured when reading from stream!" << endl;
        exit(EXIT_FAILURE);
    };

    switch (week_day) {
        case Monday: cout << "Monday\n"; break;
        case Tuesday: cout << "Tuesday\n"; break;
        case Wednesday: cout << "Wednesday\n"; break;
        case Thursday: cout << "Thursday\n"; break;
        case Friday: {
            string str = "Friday";
            cout << str << endl;
            break;
        }
        case Saturday: cout << "Saturday\n"; break;
        case Sunday: cout << "Sunday\n"; break;
        default: exit(EXIT_FAILURE);
    }

    exit(EXIT_SUCCESS);
}
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn