MATLAB学习第四天(决策语句)

MATLAB决策制定

本节内容:了解MATLAB提供的决策类型,及使用它们进行决策制定。

决策结构用来做什么?决策结构要求程序员能够使用一个或者多个的条件来对程序进行评估或者测试,沿着一条或多条语句执行,如果该条件被确定为真,则继续执行;如果条件确定是假的(false),执行其他要执行的语句。

下图是一个典型的决策结构,是大多数编程语言的一般形式:

 MATLAB 提供的决策类型如下表:

语句描述
if ... end statement一个 if ... end 语句由一个布尔表达式组成,后跟一个或多个语句。
if...else...end statement一个 if 语句可以跟随一个可选的 else 语句,当布尔表达式为false时,该语句将执行。
If... elseif...elseif...else...end statements一个 if 语句后面可以有一个(或多个)可选 elseif ... 和一个 else 语句,这对于测试各种条件非常有用。
nested if statements你可以在另一个 if  elseif 语句中使用一个 if 或 elseif 语句。
switch statement一个 switch 语句允许根据值列表对变量进行相等的测试。
nested switch statements你可以在另一个 switch 语句中使用一个 swicth 语句。

 

  • MATLAB if...end 语句

一个 if 语句和一个布尔表达式后跟一个或多个语句,由 end 语句分隔,就是一个 if ... end 语句

MATLAB if 语句语法


在MATLAB中 的 if 语句的语法是:

if <expression>
% statement(s) will execute if the boolean expression is true 
<statements>
end

表达式的计算结果如果是“true”,那么在代码块中,如果语句会被执行。如果表达式计算结果为“false”,那么第一套代码结束后的语句会被执行。

MATLAB if 语句流程图:


详细例子如下:


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

a = 10;
% check the condition using if statement 
   if a < 20 
   % if condition is true then print the following 
       fprintf('a is less than 20
' );
   end
fprintf('value of a is : %d
', a);

运行该文件,显示下述结果:

a is less than 20
value of a is : 10
  • 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中建立一个脚本文件,并输入下述的代码:

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
  • MATLAB if...elseif...elseif...else...end 语句

MATLAB 的 if...elseif...elseif...else...end 语句中 if 语句可以跟随一个(或多个)可选的 elseif... else 语句,这是非常有用的,可以用来对各种条件进行测试。

使用 if... elseif...elseif...else 语句,要注意以下几点:

  • 一个 if 可以有零个或多个 else,但是它必须跟在 elseif 后面(即只有 elseif 存在才会有 else)。 

  • 一个 if 可以有零个或多个 elseif ,必须出现else。

  • 一旦 elseif 匹配成功,余下的 elseif 将不会被测试。

 if... elseif...else...end 语法:

if <expression 1>
% Executes when the expression 1 is true 
<statement(s)>
elseif <expression 2>
% Executes when the boolean expression 2 is true
<statement(s)>
Elseif <expression 3>
% Executes when the boolean expression 3 is true 
<statement(s)>
else 
%  executes when the none of the above condition is true 
<statement(s)>
end

详细例子如下:

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

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

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

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

 

  • MATLAB嵌套if语句

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

MATLAB嵌套 if 语句语法:

详细语法如下:

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

 

  • MATLAB switch语句

MATLAB中 switch 块有条件地执行一组语句,这些语句是从几个选项里选择执行的,其中每个选项涵盖了一个 case 语句。

请记住:

  • 计算 switch_expression 是一个标量或字符串。
  • 计算 case_expression 是标量,标量或字符串的字符串或单元阵列。

switch 块的功能是测试每个 case ,直到被测试的其中一个 case 是 true 。

case 是 true 的情况如下:

  • 对于数字,eq(case_expression,switch_expression).

  • 对于字符串,strcmp(case_expression,switch_expression).

  • 对于对象,支持 eq 函数,eq(case_expression,switch_expression).

  • 对于单元阵列case_expression的,在单元阵列与switch_expression相匹配的元素中的至少一个,如上文所定义的数字,字符串和对象。

当上述有一个情况是 true,MATLAB 就执行与之相应的语句,然后不再执行以后的语句,直接退出 switch 块。

otherwise 块是可选的,任何情况下,只有当真正执行。

MATLAB switch语句语法

在MATLAB 中 switch 语句的语法如下:

switch <switch_expression>
   case <case_expression>
     <statements>
   case <case_expression>
     <statements>
     ...
     ...
   otherwise
       <statements>
end

详细例子

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

n = input('Enter a number: ');

switch n
    case -1
        disp('negative one')
    case 0
        disp('zero')
    case 1
        disp('positive one')
    otherwise
        disp('other value')
end

在命令提示符下,输入数字 1,输出结果为:

positive one

重复执行该代码并输入数字 3,输出结果为:

other value

 

  • MATLAB 嵌套switch语句

在 MATLAB 中嵌套 switch 语句是可能的,可以在 switch 一部分外嵌套 switch 语句序列。即使 case 常量的内部和外部的 switch 含有共同的值,也不算冲突出现。

MATLAB嵌套switch语句语法

嵌套switch语句的语法如下:

switch(ch1) 
   case 'A' 
   fprintf('This A is part of outer switch');
      switch(ch2) 
         case 'A'
           fprintf('This A is part of inner switch' );
          case 'B'  
          fprintf('This B is part of inner switch' );
       end   
case 'B'
fprintf('This B is part of outer switch' );
end

详细例子:

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

a = 100;
b = 200;
switch(a) 
      case 100 
         fprintf('This is part of outer switch %d
', a );
         switch(b) 
            case 200
               fprintf('This is part of inner switch %d
', b );
         end
end
fprintf('Exact value of a is : %d
', a );
fprintf('Exact value of b is : %d
', b );

当运行该文件时,它会显示:

This is part of outer switch 100
This is part of inner switch 200
Exact value of a is : 100
Exact value of b is : 200
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值