欢迎来到天天文库
浏览记录
ID:62207834
大小:71.15 KB
页数:6页
时间:2021-04-21
《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来说,可以为如下
此文档下载收益归作者所有