• MATLAB 嵌套switch语句||MATLAB while循环


    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

    MATLAB while循环

    MATLAB的 while 循环会重复执行 while  end 语句间的运算式,只要表达式为 true

    MATLAB while循环语法

    在MATLAB 中 while循环的语法如下:

    while 
       
    end

    while 循环反复执行程序语句只要表达式为 true。

    当结果不为空,并包含所有非零元素(逻辑或实际数字),表达式为 true ;否则,表达式为 false。

    详细例子

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

    a = 10;
    % while loop execution 
    while( a < 20 )
      fprintf('value of a: %d\n', a);
      a = a + 1;
    end

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

    value of a: 10
    value of a: 11
    value of a: 12
    value of a: 13
    value of a: 14
    value of a: 15
    value of a: 16
    value of a: 17
    value of a: 18
    value of a: 19
  • 相关阅读:
    SubGHz, LoRaWAN, NB-IoT, 物联网
    Spring IoC源码:createBean(下)
    CSAPP 第九章---虚拟内存
    CSS-线性渐变无畸变-环形普通进度条-环形能量块进度条-局部环形普通进度条
    机器学习实战练手项目
    springboot 整合clickhouse
    pycharm+python研究生招生智能问答系统django+vue
    spring boot jar 启动报错 Zip64 archives are not supported
    C#生成putty格式的ppk文件(支持passphrase)
    工具 - markdown编辑器常用方法
  • 原文地址:https://blog.csdn.net/m0_69824302/article/details/134519429