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"
      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
tcl_decisions.htm
廣告
© . All rights reserved.