详细内容

Tcl-决策

时间:2025-05-10     作者:邱新龙翻译【转载】   来自:vmd手册   阅读

Tcl决策

决策结构需要程序员指定的一个或多个条件进行评估,或由程序进行测试,如果条件被确定为真以及一条或多条语句,任选的其它语句,如果条件被确定为假则被执行。


以下是在大多数编程语言中找到的典型决策结构的一般形式:

TCL语言使用expr内部命令,因此它为我们声明使用expr的声明不是必需的。

TCL语言提供了以下几种类型的决策语句。

image.png

语名

描述

if语句

if语句包含一个布尔表达式后跟一个或多个语句。

if...else语句

if语句可以跟着一个可选的else语句,当else执行时,布尔表达式是假。

内嵌if语句

可以使用一个if else if 里面再声明 if 或 else if 语句(S)

switch语句

switch语句可以让一个变量对值列表相等进行测试。

内嵌switch语句

可以使用一个switch语句在另一个switch语句中。


 ? : 操作符

我们已经覆盖条件操作符 ? :在前面的章节中可以用它来代替 if ... else语句。以下是它的一般形式:

Exp1 ? Exp2 : Exp3;

计算Exp1,Exp2和Exp3表达式。注意使用和放置。


a的值?表达是确定这样:Exp1计算评估。如果是真的,那么Exp2后进行评估计算,并成为整个的值?表达式。如果计算Exp1是假的,那么Exp3计算它的值变为表达式的值。一个例子如下所示。

#!/usr/bin/tclsh

set a 10;

set b [expr $a == 1 ? 20: 30]

puts "Value of b is $b\n"

set b [expr $a == 10 ? 20: 30]

puts "Value of b is $b\n" 


当编译和执行上面的程序,将会产生以下结果:

Value of b is 30

Value of b is 20


Tcl if语句

if语句包含一个布尔表达式后跟一个或多个语句。

Tcl语言的if语句的语法是:

if {boolean_expression} {

   # statement(s) will execute if the boolean expression is true

}

如果代码里布尔表达式的值为真,那么if语句块将被执行。如果 if 语句的结束(右大括号后)布尔表达式的值为false,那么第一套代码会被执行。

TCL语言使用expr内部命令,因此它不是明确地使用expr语句声明所需的。

image.png


#!/usr/bin/tclsh

set a 10

#check the boolean condition using if statement

if { $a < 20 } {

   # if condition is true then print the following 

   puts "a is less than 20" 

}

puts "value of a is : $a" 

当上述代码被编译和执行时,它产生了以下结果:

a is less than 20

value of a is : 10


Tcl if...else 语句

if语句可以跟着一个可选的else语句,else语句块执行时,布尔表达式是假的。

在Tcl语言的if ... else语句的语法是:

if {boolean_expression} {

  # statement(s) will execute if the boolean expression is true 

} else {

  # statement(s) will execute if the boolean expression is false

}

如果布尔表达式的值为true,那么if代码块将被执行,否则else块将被执行。

TCL语言使用expr内部命令,因此它不是明确地使用expr语句所需的。

image.png


#!/usr/bin/tclsh

set a 100

#check the boolean condition 

if {$a < 20 } {

   #if condition is true then print the following 

   puts "a is less than 20"

} else {

   #if condition is false then print the following 

   puts "a is not less than 20"

}

puts "value of a is : $a"

当上述代码被编译和执行时,它产生了以下结果:

a is not less than 20;

value of a is : 100


if...else if...else 语句

if语句可以跟着一个可选的else if ... else语句,使用单个if 测试各种条件if...else if 声明是非常有用的。

当使用if , else if , else语句有几点要记住:

一个if可以有零或一个else,它必须跟在else if之后。

一个if语句可以有零到多个else if,并且它们必须在else之前。

一旦一个 else if 成功,任何剩余else if 或else 不会再被测试。


Tcl语言的 if...else if...else语句的语法是:

if {boolean_expression 1} {

   # Executes when the boolean expression 1 is true

} elseif {boolean_expression 2} {

   # Executes when the boolean expression 2 is true 

} elseif {boolean_expression 3} {

   # Executes when the boolean expression 3 is true 

} else {

   # executes when the none of the above condition is true 

}


#!/usr/bin/tclsh

 set a 100

#check the boolean condition

if { $a == 10 } { # if condition is true then print the following

puts "Value of a is 10"

} elseif { $a == 20 } {# if else if condition is true 

puts "Value of a is 20"

} elseif { $a == 30 } { # if else if condition is true 

   puts "Value of a is 30"

} else { # if none of the conditions is true 

   puts "None of the values is matching"

}

puts "Exact value of a is: $a"


当上述代码被编译和执行时,它产生了以下结果:

None of the values is matching

Exact value of a is: 100


嵌套if语句

Tcl嵌套if-else语句它始终是合法的,这意味着可以使用一个 if 或 else if 在另一 if 或 else if 声明中。

嵌套if语句的语法如下:


if { boolean_expression 1 } {

   # Executes when the boolean expression 1 is true 

   if {boolean_expression 2} {

      # Executes when the boolean expression 2 is true

   }

}

可以嵌套else if...else 在类似嵌套if语句的方式。


示例

#!/usr/bin/tclsh

set a 100

set b 200

# check the boolean condition 

if { $a == 100 } {

   # if condition is true then check the following 

   if { $b == 200 } {

      #if condition is true then print the following 

      puts "Value of a is 100 and b is 200"

   }

}

puts "Exact value of a is : $a"

puts "Exact value of b is : $b"


当上述代码被编译和执行时,它产生了以下结果:


Value of a is 100 and b is 200

Exact value of a is : 100

Exact value of b is : 200



Switch语句

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

Tcl语言未加引号的switch语句的语法如下:

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

Tcl语言未加引号的switch语句的语法如下:

switch switchingString {

   matchString1 {

      body1

   }

   matchString2 {

      body2

   }

...

   matchStringn {

      bodyn

   }

}

以下规则适用于switch语句:

在switch语句中使用的switchingString通过比较matchString使用在不同块之间。


在一个switch内可以任何数量matchString块。

switch语句可以具有可选默认块,其中必须出现在开关的末尾。缺省情况下,可用于执行任务时没有一个case是真实的。

image.png


#!/usr/bin/tclsh

 

set grade C;

 

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"

当上述代码被编译和执行时,它产生了以下结果:


You passed!

Your grade is  C



#!/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"

当上述代码被编译和执行时,它产生了以下结果:


Well done

Your grade is B


#!/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


最新评论
请先登录才能进行回复登录
技术支持: CLOUD | 管理登录
seo seo