在 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
結構的基本邏輯。請注意,接下來的程式碼示例演示了沒有任何 break
的 switch
語句,這將表現得相當突兀。如果 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);
}
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