switch语句可以让一个变量值的列表进行相等测试。每个值被称为一个的情况(case),该变量被接通检查每个switch case

switch - 语法

switch switchingString {
   matchString1 {
      body1
   }
   matchString2 {
      body2
   }
...
   matchStringn {
      bodyn
   }
}

switch - 流程图

Switch Statement

switch -示例

#!/usr/bin/tclsh

set grade B;

switch $grade {
   A {
      puts "Well done!"
   }
   B {
      puts "Excellent!"
   }

   C {
      puts "You passed!"
   }
   F {
      puts "Better try again"
   }
   default {
      puts "Invalid grade"
   }
}
puts "Your grade is  $grade"

编译并执行上述代码后,将产生以下输出-

Excellent!
Your grade is  B

参考链接

https://www.learnfk.com/tcl-tk/tcl-switch-statement.html