JSRUN 用代码说话

嵌套if语句

编辑教程

嵌套if语句

在MATLAB中嵌套if语句始终是合法的,也就是说可以使用一个嵌套的 if-else语句 if 或 elseif 语句在另一个 if 或 elseif 语句。

语法:

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

可以嵌套 elseif 或其他类似的方式,因为已经嵌套 if 语句。

详细例子

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

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
' );
       end

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

运行该文件,它显示的结果如下:

Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200
JSRUN闪电教程系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的闪电教程,让知识的积累变得统一完整、自成体系。 大家可以一起参与进共编,让零散的知识点帮助更多的人。
X
支付宝
9.99
无法付款,请点击这里
金额: 0
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
如有疑问请联系QQ:565830900
正在生成二维码, 此过程可能需要15秒钟