MATLAB 嵌套switch语句

时间:2022-06-16 19:08:24

这是可能的的switch 一部分外 switch 语句序列。即使case 常量的内部和外部的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

例子:

创建一个脚本文件,并键入下面的代码:

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', a );
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 100
Exact value of a is : 100
Exact value of b is : 200