TCL基础教程——(五)TCL中的结构控制.docx

TCL基础教程——(五)TCL中的结构控制.docx

ID:62207834

大小:71.15 KB

页数:6页

时间:2021-04-21

TCL基础教程——(五)TCL中的结构控制.docx_第1页
TCL基础教程——(五)TCL中的结构控制.docx_第2页
TCL基础教程——(五)TCL中的结构控制.docx_第3页
TCL基础教程——(五)TCL中的结构控制.docx_第4页
TCL基础教程——(五)TCL中的结构控制.docx_第5页
资源描述:

《TCL基础教程——(五)TCL中的结构控制.docx》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库

1、TCL基础教程——(5)TCL中的结构控制TCL中的控制结构是通过使用命令来实现的,命令中有循环命令:while,foreach和for。还有条件命令:if和switch。错误处理命令:catch。还有一些控制微调结构的命令,如:break,continue,return和error。一.ifthenelse这个命令的语法为ifespressionthenbody1elsebody2看这个程序:[ppcorn@localhostppcorn]$catiftest1.tcl#!/usr/bin/tclsh#######################

2、###############################Thisprogramusedtotestiftheneles#Thenumberinputbykeyboardwillbedivideby10####################################################puts-nonewline"Pleaseinputanumber:"flushstdout;setx[getsstdin]if{$x==0}then{putsstderr"Dividebyzero"}else{setslope[expr10/$x

3、]puts$slope}[ppcorn@localhostppcorn]$./iftest1.tclPleaseinputanumber:0Dividebyzero[ppcorn@localhostppcorn]$./iftest1.tclPleaseinputanumber:25这个程序中,请大家注意一下读入键盘输入的方法,先执行flushstdout,然后使用来读键盘输入。还有一个需要注意的是在程序的第一个字符为#的话,表示这行被注释。getstdin同时,在这个结构中,then是可以省略的,也就是程序也可以是这个样子[ppcorn@localh

4、ostppcorn]$catiftest2.tcl#!/usr/bin/tclsh######################################################Thisprogramusedtotestifeles#Thenumberinputbykeyboardwillbedivideby10####################################################puts-nonewline"Pleaseinputanumber:"flushstdout;setx[getsstdin]if

5、{$x==0}{putsstderr"Dividebyzero"}else{setslope[expr10/$x]puts$slope}除了ifelse外,还有ifelseifelse的用法,用来处理有更多可能结果的情况,如下面的这个程序[ppcorn@localhostppcorn]$catiftest3.tcl#!/usr/bin/tclsh######################################################Thisprogramusedtotestifelesif#Thenumberinputbykeybo

6、ardwillbecomparedby0####################################################puts-nonewline"Pleaseinputanumber:"flushstdout;setx[getsstdin]if{$x<0}then{puts"Theinputnumber$xlessthan0"}elseif{$x==0}{puts"Theinputnumber$xequal0"}else{puts"Theinputnumber$xlargethan0"}[ppcorn@localhostpp

7、corn]$./iftest3.tclPleaseinputanumber:-1Theinputnumber-1lessthan0[ppcorn@localhostppcorn]$./iftest3.tclPleaseinputanumber:0Theinputnumber0equal0[ppcorn@localhostppcorn]$./iftest3.tclPleaseinputanumber:1Theinputnumber1largethan0二.switchswitch命令根据表达式的值的不同来执行多个分支命令中的一个,该命令的一般形式为:sw

8、itchflagsvaluepat1body1pat2body2对于flags来说,可以为如下

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。