MATLAB 嵌套的switch语句
在外部switch语句的语句序列中可以有一个switch。即使内部和外部switch的case常量包含相同的值,也不会发生冲突。
语法
嵌套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\n', a );
switch(b)
case 200
fprintf('This is part of inner switch %d\n', a );
end
end
fprintf('Exact value of a is : %d\n', a );
fprintf('Exact value of b is : %d\n', 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