Matlab-if-else-statement-matlab
提供:Dev Guides
MATLAB-if … else … endステートメント
ifステートメントの後にオプションのelseステートメントを続けることができます。これは、式が偽の場合に実行されます。
構文
MATLABのif … elseステートメントの構文は-
if <expression>
% statement(s) will execute if the boolean expression is true
<statement(s)>
else
<statement(s)>
% statement(s) will execute if the boolean expression is false
end
ブール式の評価がtrueの場合、コードのifブロックが実行され、そうでない場合はコードのブロックが実行されます。
流れ図
例
スクリプトファイルを作成し、次のコードを入力します-
a = 100;
% check the boolean condition
if a < 20
% if condition is true then print the following
fprintf('a is less than 20\n' );
else
% if condition is false then print the following
fprintf('a is not less than 20\n' );
end
fprintf('value of a is : %d\n', a);
上記のコードをコンパイルして実行すると、次の結果が生成されます-
a is not less than 20
value of a is : 100