TCL基础教程——(5)TCL中的结构控制_第1页
TCL基础教程——(5)TCL中的结构控制_第2页
TCL基础教程——(5)TCL中的结构控制_第3页
TCL基础教程——(5)TCL中的结构控制_第4页
TCL基础教程——(5)TCL中的结构控制_第5页
已阅读5页,还剩5页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 TCL基础教程(5)TCL中的结构控制 TCL中的控制结构是通过使用命令来实现的,命令中有循环命令:while,foreach和for。还有条件命令:if和switch。错误处理命令:catch。还有一些控制微调结构的命令,如:break,continue,return和error。 一if then else 这个命令的语法为 if espression then body1 else body2 看这个程序: ppcornlocalhost ppcorn$ cat iftest1.tcl #!/usr/bin/tclsh # # This program used to test if

2、then eles # The number input by keyboard will be divide by 10 # puts -nonewline Please input a number: flush stdout; set x gets stdin if $x=0 then puts stderr Divide by zero else set slope expr 10/$x puts $slope ppcornlocalhost ppcorn$ ./iftest1.tcl Please input a number: 0 Divide by zero ppcornloca

3、lhost ppcorn$ ./iftest1.tcl Please input a number: 2 5 这个程序中,请大家注意一下读入键盘输入的方法,先执行flush stdout,然后使用get stdin来读键盘输入。 还有一个需要注意的是在程序的第一个字符为#的话,表示这行被注释。 同时,在这个结构中,then是可以省略的,也就是程序也可以是这个样子 ppcornlocalhost ppcorn$ cat iftest2.tcl #!/usr/bin/tclsh # # This program used to test if eles # The number input by

4、keyboard will be divide by 10 # puts -nonewline Please input a number: flush stdout; set x gets stdin if $x=0 puts stderr Divide by zero else set slope expr 10/$x puts $slope 除了if else外,还有if elseif else的用法,用来处理有更多可能结果的情况,如下面的这个程序 ppcornlocalhost ppcorn$ cat iftest3.tcl #!/usr/bin/tclsh # # This prog

5、ram used to test if elesif # The number input by keyboard will be compared by 0 # puts -nonewline Please input a number: flush stdout; set x gets stdin if $x<0 then puts The input number $x less than 0 elseif $x=0 puts The input number $x equal 0 else puts The input number $x large than 0 ppcornl

6、ocalhost ppcorn$ ./iftest3.tcl Please input a number: -1 The input number -1 less than 0 ppcornlocalhost ppcorn$ ./iftest3.tcl Please input a number: 0 The input number 0 equal 0 ppcornlocalhost ppcorn$ ./iftest3.tcl Please input a number: 1 The input number 1 large than 0 二switch switch命令根据表达式的值的不同

7、来执行多个分支命令中的一个,该命令的一般形式为: switch flags value pat1 body1 pat2 body2 对于flags来说,可以为如下值: 精确匹配,也是默认值-exact -glob 使用通配符的格式 -regexp 使用正则表达式匹配 - 没有标志(或者标志结束)。当value以-开始的时候,必须用到这个。 看一个使用精确匹配的例子 ppcornlocalhost ppcorn$ cat switchtest1.tcl #!/usr/bin/tclsh # # This program used to test switch # The number input

8、 will be comared with 0,10,100 # puts -nonewline Please input a number: flush stdout; set x gets stdin switch -exact $x 0 puts The input is 0 10 puts The input is 10 100 puts The input is 100 default puts Hello ppcornlocalhost ppcorn$ ./switchtest1.tcl Please input a number: 0 The input is 0 ppcornl

9、ocalhost ppcorn$ ./switchtest1.tcl Please input a number: 10 The input is 10 ppcornlocalhost ppcorn$ ./switchtest1.tcl Please input a number: 100 The input is 100 ppcornlocalhost ppcorn$ ./switchtest1.tcl Please input a number: 90 Hello 在上个程序中,如果读入的是0,10,100,分别执行各自内部的指令,如果都不是,则执行默认的指令,也就是default内部的。

10、 再看一个使用通配符的程序 ppcornlocalhost ppcorn$ cat switchtest2.tcl #!/usr/bin/tclsh # # This program used to test switch # The word input will be comared with a,e,i,o,u # puts -nonewline Please input word: flush stdout; set x gets stdin switch -glob $x *a* puts The word has alpha a *e* puts The word has alph

11、a e *i* puts The word has alpha i *o* puts The word has alpha o *u* puts The word has alpha u default puts The word is error ppcornlocalhost ppcorn$ ./switchtest2.tcl Please input word: pat The word has alpha a ppcornlocalhost ppcorn$ ./switchtest2.tcl Please input word: bed The word has alpha e ppc

12、ornlocalhost ppcorn$ ./switchtest2.tcl Please input word: bit The word has alpha i ppcornlocalhost ppcorn$ ./switchtest2.tcl Please input word: old The word has alpha o ppcornlocalhost ppcorn$ ./switchtest2.tcl Please input word: push The word has alpha u ppcornlocalhost ppcorn$ ./switchtest2.tcl Pl

13、ease input word: rt The word is error 三while while接受两个变元,一个是测试表达式,一个命令体,如: while booleanExpr body 看下面的程序,输入一个数字后,计算1到该数字的累加之和。 ppcornlocalhost ppcorn$ cat whiletest.tcl #!/usr/bin/tclsh # # This program used to test while # The program will add from 1 to the number input by keyboard # puts -nonewlin

14、e Please input a number: flush stdout; set x gets stdin set j 0 set i 1 while $i<$x set j expr $j+$i incr i puts $j ppcornlocalhost ppcorn$ ./whiletest.tcl Please input a number: 10 45 请大家注意程序中incr i的用法,这个表示自动给i加1,类似别的语言中的i+,如果每次都要自动加2,则写为incr i 2,如果是递减的话,则是incr i -1。 四for for命令的格式如下 for initial

15、test final body 将上面的while程序用for来实现 ppcornlocalhost ppcorn$ cat fortest.tcl #!/usr/bin/tclsh # # This program used to test for # The program will add from 1 to the number input by keyboard # puts -nonewline Please input a number: flush stdout; set x gets stdin set j 0 for set i 0 $i<$x incr i set j expr $j+$i puts $j ppcornlocalhost ppcorn$ ./fortest.tcl Please input a number: 10 45 五foreach foreach命令循环执行一个命令体,每次将一个或多个列表中的值赋给一个或多个变量,一般的语法为: foreach loopVar valuelist commandBody. 关于列表的用法,将在下面讲到。 看这个程序ppcornlocalhost ppcorn$ cat foreachte

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论