可能有一個(gè) switch 作為外部switch的語句序列的一部分。即使內(nèi)部和外部switch的大小寫常量包含公共值,也不會發(fā)生沖突。
嵌套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創(chuàng)建一個(gè)腳本文件并在其中鍵入以下代碼-
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 );運(yùn)行文件時(shí),它顯示-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