【文章內(nèi)容簡介】
9 || || : 邏輯或10 x?y:z ifthenelse2. 數(shù)學函數(shù)序號 函數(shù) 序號 函數(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 頁 共 75 頁 9 atan 19 sin10 exp例子:set X 100。set Y 256 。 行末是否有分號都可以set Z [expr $Y + $X] 。 變量是否被雙引號包含都可以,不過建議使用雙引號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]} 。外層是花括號不會進行置換puts $Z_LABEL {[expr $Y + $X]} 。 外層是雙引號會進行置換puts The mand to add two numbers is: \[expr \$a + \$b] 第 7 課:文本比較-SWITCH 應用講解:1. switch 的分支中的命令使用花括號包含,但是并不會影響花括號中的命令執(zhí)行,切記,這是switch 的格式;2. 如果不想分支條件進行置換,需要在外加上花括號,不會影響分支中的命令執(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 頁 共 75 頁 。下面這種寫法$z 被置換,走入$z 的條件分支,表面上看條件分支中的命令在花括號內(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, 在外層加上了花括號,于是走入了 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 應用講解:1. 條件式結(jié)果FALSE TRUE數(shù)值 0 非零yes / no no yestrue / false false true2.置換變量的方法,set y x 。 puts $$y ,因為是后結(jié)合并且是一次置換,所以打出來的是 $x ,不是$x 的值;但是在 if 的條件式中進行了二次置換, $$y 被置換成了 $x 的值3.注意:新行中需要寫為 } else { ,不能將 } 寫到前一行的末尾,也不能省略 } 后面的那個空格,后面的 { 也需要寫在當行,并且前面需要一個空格。例子: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 頁 共 75 頁 if $x==1 {puts GOT 1}set y x。if $$y != 1 { 。在 if 條件式中$$y 進行了二次置換 puts $$y is != 1 。在 puts 命令中,只進行了一次置換 } else { puts $$y is 1 } 第 9 課:WHILE 循環(huán)x 講解:1.while 后面的條件表達式是放在花括號中的;放在雙引號中會只執(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,于是該條件永遠為真set x [expr $x + 1]if {$x 6} break。 。如果去掉這句就成了死循環(huán) if $x 3 continue。 。這句使 4 打不出來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] 達到一樣的效果,向上加一x例子:for {puts Start。 set i 0} {$i 2} {incr i。 puts I after incr: $i。 } { 。第一部分只執(zhí)行一次,后面兩部分每次循環(huán)都會執(zhí)行 puts I inside first loop: $i 第 12 頁 共 75 頁 }for {puts Start。 set i 3} {$i 2} {incr i。 puts I after incr: $i。 } { 。不會執(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 課:過程 PROC講解:1. 格式:proc name args body2. 調(diào)用方法中參數(shù)可以用花括號或者雙引號包含,也可以不包含3. 在 puts 等命令中需要置換的話,需要使用方括號例子:proc sum {arg1 arg2} { set x [expr $arg1+$arg2]。 return $x 。過程返回值 }puts The sum of 2 + 3 is: [sum 2 3]\n\n 。調(diào)用過程puts The sum of 2 + 3 is: [sum {2 3}]\n\n 。出錯,提示找不到第二個參數(shù),置換過程中第一個參數(shù)是{2 3},所以找不到第二個參數(shù)puts The sum of 2 + 3 is: sum(2 3)\n\n 。輸出 sum(2 3),因為沒有方括號,根本沒有進行置換puts The sum of 2 + 3 is: sum{2 3}\n\n 。輸出 sum{2 3},因為沒有方括號,根本沒有進行置換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 頁 共 75 頁 12 課:過程 PROC 的參數(shù)定義講解:1. 過程的參數(shù)賦缺省值:proc name {arg1 {arg2 value}}2. 過程的不確定個數(shù)的參數(shù)定義:proc name {arg1 args}例子:proc example {first {second } args} { 。參數(shù)定義:賦缺省值和不確定個數(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,作用有兩個:一是將上一層的 x 的值賦給 y;二是將上一層的 x 的地址賦給 y,于是修改 y 等于修改 x。1 代表作用范圍,也可為 2,3 等,不能為 0例子:proc SetPositive {variable value } { 。此處 variable 只是一個參數(shù)名,可以修改為其他的來代替變量 upvar $variable myvar 。此處也可寫為 upvar 1 $variable myvar if {$value 0} { set myvar [expr $value]。} else {set myvar $value。} return $myvar。 第 14 頁 共 75 頁 }SetPositive x 5。SetPositive y 5。puts