matlab中if elseif语句,Matlab if…elseif…elseif…else…end语句

if语句后面可以有一个(或多个)可选elseif...和一个else语句,这对于测试各种条件非常有用。

当使用if...elseif...else语句时,请记住几点:

if可以有零个或一个else,它必须在elseif之后。

if可以有零到多个elseif,它们必须在else语句之前。

当有一个else if匹配成功,其余的elseif和else都不会被测试。

语法

if % Executes when the expression 1 is true elseif % Executes when the boolean expression 2 is true Elseif % Executes when the boolean expression 3 is true else % executes when the none of the above condition is true end

例子

创建脚本文件并在其中键入以下代码 –

a = 100; %check the boolean condition if a == 10 % if condition is true then print the following fprintf('Value of a is 10n' ); elseif( a == 20 ) % if else if condition is true fprintf('Value of a is 20n' ); elseif a == 30 % if else if condition is true fprintf('Value of a is 30n' ); else % if none of the conditions is true ' fprintf('None of the values are matchingn'); fprintf('Exact value of a is: %dn', a ); end

当上述代码被编译和执行时,它产生以下结果 –

None of the values are matching Exact value of a is: 100

¥ 我要打赏 纠错/补充 收藏

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值