Tcl 嵌套的Switch语句
有可能在外部Switch语句的语句序列中包含一个 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"
switchb {
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