matlab里else,MATLAB if...else...end 语句

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

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

if...else...end 语句语法:

MATLAB 中一个if ... else语句的语法示例:

if % statement(s) will execute if the boolean expression is true

else

% statement(s) will execute if the boolean expression is false

end

如果布尔表达式的值为 “true”,那么执行 if 的代码块;如果布尔表达式的值为 “false”,else 的代码块将被执行。

if...else...end 语句流程图:

3ab77cd2a6e106821dcda8ddaf1cc9c4.png

详细例子如下:

在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值