Matlab-nested-if-statements-matlab

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

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

MATLABでは、if-elseステートメントをネストすることは常に有効です。つまり、ifまたはelseifステートメントを別のifまたはelseifステートメント内で使用できます。

構文

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

if <expression 1>
   % Executes when the boolean expression 1 is true
   if <expression 2>
      % Executes when the boolean expression 2 is true
   end
end

ifステートメントをネストしたのと同様の方法でelseif …​ elseをネストできます。

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

a = 100;
b = 200;
   % check the boolean condition
   if( a == 100 )

      % if condition is true then check the following
      if( b == 200 )

         % if condition is true then print the following
         fprintf('Value of a is 100 and b is 200\n' );
      end

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

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

Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200