有可能有一個(gè)switch作為外開關(guān)的語(yǔ)句序列的一部分。即使在內(nèi)外switch case 的常數(shù)包含共同的值,如果沒有沖突將出現(xiàn)。
嵌套switch語(yǔ)句的語(yǔ)法如下:
switch switchingString { matchString1 { body1 switch switchingString { matchString1 { body1 } matchString2 { body2 } ... matchStringn { bodyn } } } matchString2 { body2 } ... matchStringn { bodyn } }
#!/usr/bin/tclsh set a 100 set b 200 switch $a { 100 { puts "This is part of outer switch" switch $b { 200 { puts "This is part of inner switch!" } } } } puts "Exact value of a is : $a" puts "Exact value of a is : $b"
當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生了以下結(jié)果:
This is part of outer switch This is part of inner switch! Exact value of a is : 100 Exact value of a is : 200