当前位置: 首页 > article >正文

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 <expression>
   <statements>
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

http://www.kler.cn/news/135564.html

相关文章:

  • C++中只能有一个实例的单例类
  • LeetCode Hot100之十:239.滑动窗口最大值
  • 网络运维与网络安全 学习笔记2023.11.19
  • 【Go学习之 go mod】gomod小白入门,在github上发布自己的项目(项目初始化、项目发布、项目版本升级等)
  • 世界坐标系,相机坐标系,像素坐标系转换 详细说明(附代码)
  • PCIe协议加持,SD卡9.1规范达到媲美SSD的速度4GB/s
  • 【设计模式】聊聊模板模式
  • 解析Spring Boot中的CommandLineRunner和ApplicationRunner:用法、区别和适用场景详解
  • CISP全真模式测试题(二)
  • VSCode使用
  • 基于Vue+SpringBoot的超市账单管理系统 开源项目
  • CentOS 7.9 安装 epel-release
  • cobol基本语法
  • k8s-部署Redis-cluster(TLS)
  • 【React】React 基础
  • 基础算法:高精度加法
  • C语言之深入指针及qsort函数(五)(详解介绍)
  • 微信小程序内嵌h5页面,实现动态设置顶部标题的功能
  • ArkTS - HarmonyOS服务卡片(创建)
  • CISP模拟试题(一)
  • uniapp+vue+Springboot 公司网站0~1搭建 前端前期设计篇
  • 串行通信中的同步方式(Synchronous)与异步方式(Asynchronous)stty -F设置波特率
  • “移动机器人课程群实践创新的困境与突围”素材
  • 动态页面调研及设计方案
  • 【Java 进阶篇】Ajax 实现——原生JS方式
  • 文件传输客户端 SecureFX mac中文版支持多种协议
  • 归并排序详解:递归实现+非递归实现(图文详解+代码)
  • 设计模式-组合模式-笔记
  • 应试教育导致学生迷信标准答案惯性导致思维僵化-移动机器人
  • Android描边外框stroke边线、rotate旋转、circle圆形图的简洁通用方案,基于Glide与ShapeableImageView,Kotlin