有可能有一个switch作为外开关的语句序列的一部分。即使在内外switch case 的常数包含共同的值,如果没有冲突将出现。
嵌套switch语句的语法如下:
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"
当上述代码被编译和执行时,它产生了以下结果:
This is part of outer switch This is part of inner switch! Exact value of a is : 100 Exact value of a is : 200