【文章內(nèi)容簡(jiǎn)介】
9 || || : 邏輯或10 x?y:z ifthenelse2. 數(shù)學(xué)函數(shù)序號(hào) 函數(shù) 序號(hào) 函數(shù)1 acos 11 log102 cos 12 tan3 hypot 13 atan24 sinh 14 floor5 asin 15 pow6 cosh 16 tanh7 log 17 ceil8 sqrt 18 fmod 第 9 頁(yè) 共 75 頁(yè) 9 atan 19 sin10 exp例子:set X 100。set Y 256 。 行末是否有分號(hào)都可以set Z [expr $Y + $X] 。 變量是否被雙引號(hào)包含都可以,不過(guò)建議使用雙引號(hào)set Z [expr $Y + $X]set Z_LABEL $Y plus $X is puts $Z_LABEL $Zputs The square root of $Y is [expr sqrt($Y)]\nputs Because of the precedence rules \5 + 3 * 4\ is: [expr 3 * 4 + 5]puts Because of the parentheses \(5 + 3) * 4\ is: [expr (5 + 3) * 4]puts \n................. more examples of differences between \ and \{puts {$Z_LABEL [expr $Y + $X]} 。外層是花括號(hào)不會(huì)進(jìn)行置換puts $Z_LABEL {[expr $Y + $X]} 。 外層是雙引號(hào)會(huì)進(jìn)行置換puts The mand to add two numbers is: \[expr \$a + \$b] 第 7 課:文本比較-SWITCH 應(yīng)用講解:1. switch 的分支中的命令使用花括號(hào)包含,但是并不會(huì)影響花括號(hào)中的命令執(zhí)行,切記,這是switch 的格式;2. 如果不想分支條件進(jìn)行置換,需要在外加上花括號(hào),不會(huì)影響分支中的命令執(zhí)行。例子:。 Set the variables we39。ll be paringset x ONE。set y 1。set z ONE。 This is legalswitch $x ONE puts ONE=1 TWO puts TWO=2 default puts NO_MATCH 。這種寫法合法,但是閱讀不便switch $x \ ONE puts ONE=1 \ TWO puts TWO=2 \ default puts NO_MATCH 。這種寫法好看一些,推薦 第 10 頁(yè) 共 75 頁(yè) 。下面這種寫法$z 被置換,走入$z 的條件分支,表面上看條件分支中的命令在花括號(hào)內(nèi),這只是 switch 的一種格式,所以其中的命令仍然被執(zhí)行了。switch $x \ $z {set y1 [expr $y+1]。 puts MATCH \$z. $y + $z is $y1 } \ ONE {set y1 [expr $y+1]。 puts MATCH ONE. $y + one is $y1} \ TWO {set y1 [expr $y+2]。 puts MATCH TWO. $y + two is $y1 } \ THREE {set y1 [expr $y+3]。 puts MATCH THREE. $y + three is $y1 } \ default {puts $x does not match any of these choices}。 This form of the mand disables variable substitution in the pattern。下面為了不置換$z, 在外層加上了花括號(hào),于是走入了 ONE 分支,而分支中的命令仍然被執(zhí)行了switch $x { $z {set y1 [expr $y+1]。 puts MATCH \$z. $y + $z is $y1 } ONE {set y1 [expr $y+1]。 puts MATCH ONE. $y + one is $y1} TWO {set y1 [expr $y+2]。 puts MATCH TWO. $y + two is $y1} THREE {set y1 [expr $y+3]。 puts MATCH THREE. $y + three is $y1} default {puts $x is NOT A MATCH} } 第 8 課:數(shù)值比較-IF 應(yīng)用講解:1. 條件式結(jié)果FALSE TRUE數(shù)值 0 非零yes / no no yestrue / false false true2.置換變量的方法,set y x 。 puts $$y ,因?yàn)槭呛蠼Y(jié)合并且是一次置換,所以打出來(lái)的是 $x ,不是$x 的值;但是在 if 的條件式中進(jìn)行了二次置換, $$y 被置換成了 $x 的值3.注意:新行中需要寫為 } else { ,不能將 } 寫到前一行的末尾,也不能省略 } 后面的那個(gè)空格,后面的 { 也需要寫在當(dāng)行,并且前面需要一個(gè)空格。例子:set x 1。if {$x == 2} {puts $x is 2} else {puts $x is not 2} 。判斷是否相等使用 ==if {$x != 1} { 。判斷是否不等使用 != puts $x is != 1 } else { puts $x is 1 } 第 11 頁(yè) 共 75 頁(yè) if $x==1 {puts GOT 1}set y x。if $$y != 1 { 。在 if 條件式中$$y 進(jìn)行了二次置換 puts $$y is != 1 。在 puts 命令中,只進(jìn)行了一次置換 } else { puts $$y is 1 } 第 9 課:WHILE 循環(huán)x 講解:1.while 后面的條件表達(dá)式是放在花括號(hào)中的;放在雙引號(hào)中會(huì)只執(zhí)行一次置換例子:set x 1。while {$x 5} {puts x is $x。 set x [expr $x + 1]}puts exited first loop with X equal to $x\nset x 0。while $x 5 { 。只執(zhí)行一次置換 15,于是該條件永遠(yuǎn)為真set x [expr $x + 1]if {$x 6} break。 。如果去掉這句就成了死循環(huán) if $x 3 continue。 。這句使 4 打不出來(lái)puts x is $x。 }puts exited second loop with X equal to $x\n 10 課:FOR 循環(huán)和 incr講解:1.incr x 和 set x [expr $x + 1] 達(dá)到一樣的效果,向上加一x例子:for {puts Start。 set i 0} {$i 2} {incr i。 puts I after incr: $i。 } { 。第一部分只執(zhí)行一次,后面兩部分每次循環(huán)都會(huì)執(zhí)行 puts I inside first loop: $i 第 12 頁(yè) 共 75 頁(yè) }for {puts Start。 set i 3} {$i 2} {incr i。 puts I after incr: $i。 } { 。不會(huì)執(zhí)行循環(huán)體中的命令 puts I inside second loop: $i }puts Start。 set i 0。while {$i 2} { puts I inside first loop: $i incr i。 puts I after incr: $i。 } 11 課:過(guò)程 PROC講解:1. 格式:proc name args body2. 調(diào)用方法中參數(shù)可以用花括號(hào)或者雙引號(hào)包含,也可以不包含3. 在 puts 等命令中需要置換的話,需要使用方括號(hào)例子:proc sum {arg1 arg2} { set x [expr $arg1+$arg2]。 return $x 。過(guò)程返回值 }puts The sum of 2 + 3 is: [sum 2 3]\n\n 。調(diào)用過(guò)程puts The sum of 2 + 3 is: [sum {2 3}]\n\n 。出錯(cuò),提示找不到第二個(gè)參數(shù),置換過(guò)程中第一個(gè)參數(shù)是{2 3},所以找不到第二個(gè)參數(shù)puts The sum of 2 + 3 is: sum(2 3)\n\n 。輸出 sum(2 3),因?yàn)闆](méi)有方括號(hào),根本沒(méi)有進(jìn)行置換puts The sum of 2 + 3 is: sum{2 3}\n\n 。輸出 sum{2 3},因?yàn)闆](méi)有方括號(hào),根本沒(méi)有進(jìn)行置換sum 2 3 。正確sum {2} {3} 。正確sum 2 3 。正確proc for {a b c} {puts The for mand has been replaced by a puts。puts The arguments were: $a\n$b\n$c\n}for {set i 1} {$i 10} {incr i} 第 13 頁(yè) 共 75 頁(yè) 12 課:過(guò)程 PROC 的參數(shù)定義講解:1. 過(guò)程的參數(shù)賦缺省值:proc name {arg1 {arg2 value}}2. 過(guò)程的不確定個(gè)數(shù)的參數(shù)定義:proc name {arg1 args}例子:proc example {first {second } args} { 。參數(shù)定義:賦缺省值和不確定個(gè)數(shù)參數(shù)定義 if {$second == } { puts There is only one argument and it is: $first。 return 1。 } else { if {$args == } { puts There are two arguments $first and $second。 return 2。 } else { puts There are many arguments $first and $second and $args。 return many。 } } }set count1 [example ONE]set count2 [example ONE TWO]set count3 [example ONE TWO THREE ]set count4 [example ONE TWO THREE FOUR]puts The example was called with $count1, $count2, $count3, and $count4 Arguments 13 課:變量的作用域x 講解:1. 全局變量定義:global var12. 局部變量:upvar x y 等同于 upvar 1 x y,作用有兩個(gè):一是將上一層的 x 的值賦給 y;二是將上一層的 x 的地址賦給 y,于是修改 y 等于修改 x。1 代表作用范圍,也可為 2,3 等,不能為 0例子:proc SetPositive {variable value } { 。此處 variable 只是一個(gè)參數(shù)名,可以修改為其他的來(lái)代替變量 upvar $variable myvar 。此處也可寫為 upvar 1 $variable myvar if {$value 0} { set myvar [expr $value]。} else {set myvar $value。} return $myvar。 第 14 頁(yè) 共 75 頁(yè) }SetPositive x 5。SetPositive y 5。puts