Cplusplus-cpp-nested-switch

提供:Dev Guides
移動先:案内検索

C ++のネストされたスイッチステートメント

外部スイッチのステートメントシーケンスの一部としてスイッチを持つことができます。 内側と外側のスイッチのケース定数に共通の値が含まれていても、競合は発生しません。

C ++は、switchステートメントに少なくとも256レベルのネストを許可することを指定しています。

構文

  • ネストされたスイッチ*ステートメントの構文は次のとおりです-
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