MATLAB if…else…end 语句

在MATLAB的 if…else…end 语句中,if 语句后面可以跟一个可选择的 else 语句,当执行的表达式为假的时候,执行 else 语句。

if…else…end 语句语法:


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 的代码块;如果布尔表达式的值为 “false”,else 的代码块将被执行。

if…else…end 语句流程图:


MATLAB if...else...end 语句

详细例子如下:


在MATLAB中建立一个脚本文件,并输入下述的代码:

a = 100;
% check the boolean condition 
   if a < 20 
        % if condition is true then print the following 
       fprintf(a is less than 20
 );
   else
       % if condition is false then print the following 
       fprintf(a is not less than 20
 );
   end
   fprintf(value of a is : %d
, a);

编译和执行上述代码,产生下述结果:

a is not less than 20
value of a is : 100

作者:唐伯虎点蚊香,如若转载,请注明出处:https://www.web176.com/matlab/20125.html

(0)
打赏 支付宝 支付宝 微信 微信
唐伯虎点蚊香的头像唐伯虎点蚊香
上一篇 2023年5月14日
下一篇 2023年5月14日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注