Matlab-nested-switch-statements-matlab

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

MATLAB-入れ子になったswitchステートメント

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

構文

ネストされたswitch文の構文は次のとおりです-

switch(ch1)
   case 'A'
      fprintf('This A is part of outer switch');
      switch(ch2)
         case 'A'
         fprintf('This A is part of inner switch' );

         case 'B'
         fprintf('This B is part of inner switch' );
      end
   case 'B'
      fprintf('This B is part of outer switch' );
end

スクリプトファイルを作成し、その中に次のコードを入力します-

a = 100;
b = 200;
switch(a)
   case 100
      fprintf('This is part of outer switch %d\n', a );
      switch(b)
         case 200
            fprintf('This is part of inner switch %d\n', a );
      end
end

fprintf('Exact value of a is : %d\n', a );
fprintf('Exact value of b is : %d\n', b );

あなたがファイルを実行すると、それが表示されます-

This is part of outer switch 100
This is part of inner switch 100
Exact value of a is : 100
Exact value of b is : 200