
- C++基礎
- C++首頁
- C++概述
- C++環境設定
- C++基本語法
- C++註釋
- C++ Hello World
- C++省略名稱空間
- C++常量/字面量
- C++關鍵字
- C++識別符號
- C++資料型別
- C++數值資料型別
- C++字元資料型別
- C++布林資料型別
- C++變數型別
- C++變數作用域
- C++多個變數
- C++基本輸入/輸出
- C++修飾符型別
- C++儲存類
- C++運算子
- C++數字
- C++列舉
- C++引用
- C++日期和時間
- C++控制語句
- C++決策制定
- C++ if語句
- C++ if else語句
- C++巢狀if語句
- C++ switch語句
- C++巢狀switch語句
- C++迴圈型別
- C++ while迴圈
- C++ for迴圈
- C++ do while迴圈
- C++ foreach迴圈
- C++巢狀迴圈
- C++ break語句
- C++ continue語句
- C++ goto語句
- C++建構函式
- C++建構函式和解構函式
- C++複製建構函式
C++巢狀switch語句
可以在外部switch語句的語句序列中包含一個switch語句。即使內部和外部switch語句的case常量包含公共值,也不會出現衝突。
C++規定至少允許256級巢狀switch語句。
語法
巢狀switch語句的語法如下所示:
switch(ch1) { case 'A': cout << "This A is part of outer switch"; switch(ch2) { case 'A': cout << "This A is part of inner switch"; break; case 'B': // ... } break; case 'B': // ... }
示例
#include <iostream> using namespace std; int main () { // local variable declaration: int a = 100; int b = 200; switch(a) { case 100: cout << "This is part of outer switch" << endl; switch(b) { case 200: cout << "This is part of inner switch" << endl; } } cout << "Exact value of a is : " << a << endl; cout << "Exact value of b is : " << b << endl; return 0; }
這將產生以下結果:
This is part of outer switch This is part of inner switch Exact value of a is : 100 Exact value of b is : 200
廣告