初识Tcl(四):Tcl 决策
目录
Tcl决策
? : 操作符
Tcl if语句
语法
流程图
示例
Tcl if...else 语句
语法
流程图
示例
if...else if...else 语句
语法
示例
Tcl 嵌套if语句
语法
示例
Tcl Switch语句
语法
流程图
例如:未加引号版本
例如:引用版本
Tcl嵌套Switch语句
语法
示例
Tcl决策
决策结构需要程序员指定的一个或多个条件进行评估,或由程序进行测试,如果条件被确定为真以及一条或多条语句,任选的其它语句,如果条件被确定为假则被执行。
以下是在大多数编程语言中找到的典型决策结构的一般形式:
TCL语言使用expr内部命令,因此它为我们声明使用expr的声明不是必需的。
TCL语言提供了以下几种类型的决策语句。
语名 | 描述 |
---|---|
if语句 | if语句包含一个布尔表达式后跟一个或多个语句。 |
if...else语句 | if语句可以跟着一个可选的else语句,当else执行时,布尔表达式是假。 |
内嵌if语句 | 可以使用一个if 或else if 里面再声明 if 或 else if 语句(S)。 |
switch语句 | switch语句可以让一个变量对值列表相等进行测试。 |
内嵌switch语句 | 可以使用一个switch语句在另一个switch语句中。 |
? : 操作符
我们已经覆盖条件操作符 ? :在前面的章节中可以用它来代替 if ... else语句。以下是它的一般形式:
Exp1 ? Exp2 : Exp3;
计算Exp1,Exp2和Exp3表达式。注意使用和放置。
a的值?表达是确定这样:Exp1计算评估。如果是真的,那么Exp2后进行评估计算,并成为整个的值?表达式。如果计算Exp1是假的,那么Exp3计算它的值变为表达式的值。一个例子如下所示。
#!/usr/bin/tclshset a 10;
set b [expr $a == 1 ? 20: 30]
puts "Value of b is $b\n"
set b [expr $a == 10 ? 20: 30]
puts "Value of b is $b\n"
当编译和执行上面的程序,将会产生以下结果:
Value of b is 30
Value of b is 20
Tcl if语句
if语句包含一个布尔表达式后跟一个或多个语句。
语法
Tcl语言的if语句的语法是:
if {boolean_expression} {# statement(s) will execute if the boolean expression is true
}
如果代码里布尔表达式的值为真,那么if语句块将被执行。如果 if 语句的结束(右大括号后)布尔表达式的值为false,那么第一套代码会被执行。
TCL语言使用expr内部命令,因此它不是明确地使用expr语句声明所需的。
流程图
示例
#!/usr/bin/tclshset a 10#check the boolean condition using if statement
if { $a < 20 } {# if condition is true then print the following puts "a is less than 20"
}
puts "value of a is : $a"
当上述代码被编译和执行时,它产生了以下结果:
a is less than 20
value of a is : 10
Tcl if...else 语句
if语句可以跟着一个可选的else语句,else语句块执行时,布尔表达式是假的。
语法
在Tcl语言的if ... else语句的语法是:
if {boolean_expression} {# statement(s) will execute if the boolean expression is true
} else {# statement(s) will execute if the boolean expression is false
}
如果布尔表达式的值为true,那么if代码块将被执行,否则else块将被执行。
TCL语言使用expr内部命令,因此它不是明确地使用expr语句所需的。
流程图
示例
#!/usr/bin/tclshset a 100#check the boolean condition
if {$a < 20 } {#if condition is true then print the following puts "a is less than 20"
} else {#if condition is false then print the following puts "a is not less than 20"
}
puts "value of a is : $a"
当上述代码被编译和执行时,它产生了以下结果:
a is not less than 20;
value of a is : 100
if...else if...else 语句
if语句可以跟着一个可选的else if ... else语句,使用单个if 测试各种条件if...else if 声明是非常有用的。
当使用if , else if , else语句有几点要记住:
一个if可以有零或一个else,它必须跟在else if之后。
一个if语句可以有零到多个else if,并且它们必须在else之前。
一旦一个 else if 成功,任何剩余else if 或else 不会再被测试。
语法
Tcl语言的 if...else if...else语句的语法是:
if {boolean_expression 1} {# Executes when the boolean expression 1 is true
} elseif {boolean_expression 2} {# Executes when the boolean expression 2 is true
} elseif {boolean_expression 3} {# Executes when the boolean expression 3 is true
} else {# executes when the none of the above condition is true
}
示例
#!/usr/bin/tclshset a 100#check the boolean condition
if { $a == 10 } {# if condition is true then print the following puts "Value of a is 10"
} elseif { $a == 20 } {# if else if condition is true puts "Value of a is 20"
} elseif { $a == 30 } {# if else if condition is true puts "Value of a is 30"
} else {# if none of the conditions is true puts "None of the values is matching"
}puts "Exact value of a is: $a"
当上述代码被编译和执行时,它产生了以下结果:
None of the values is matching
Exact value of a is: 100
Tcl 嵌套if语句
Tcl嵌套if-else语句它始终是合法的,这意味着可以使用一个 if 或 else if 在另一 if 或 else if 声明中。
语法
嵌套if语句的语法如下:
if { boolean_expression 1 } {# Executes when the boolean expression 1 is true if {boolean_expression 2} {# Executes when the boolean expression 2 is true}
}
可以嵌套else if...else 在类似嵌套if语句的方式。
示例
#!/usr/bin/tclshset a 100
set b 200# check the boolean condition
if { $a == 100 } {# if condition is true then check the following if { $b == 200 } {#if condition is true then print the following puts "Value of a is 100 and b is 200"}
}
puts "Exact value of a is : $a"
puts "Exact value of b is : $b"
当上述代码被编译和执行时,它产生了以下结果:
Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200
Tcl Switch语句
switch语句可以让一个变量值的列表进行相等测试。每个值被称为一个的情况(case),该变量被接通检查每个switch case。
语法
Tcl语言未加引号的switch语句的语法如下:
switch switchingString matchString1 {body1} matchString2 {body2} ... matchStringn {bodyn}
Tcl语言未加引号的switch语句的语法如下:
switch switchingString {matchString1 {body1}matchString2 {body2}
...matchStringn {bodyn}
}
以下规则适用于switch语句:
在switch语句中使用的switchingString通过比较matchString使用在不同块之间。
在一个switch内可以任何数量matchString块。
switch语句可以具有可选默认块,其中必须出现在开关的末尾。缺省情况下,可用于执行任务时没有一个case是真实的。
流程图
例如:未加引号版本
#!/usr/bin/tclshset grade C;switch $grade A { puts "Well done!" } B { puts "Excellent!" } C { puts "You passed!" } F { puts "Better try again" } default { puts "Invalid grade" }
puts "Your grade is $grade"
当上述代码被编译和执行时,它产生了以下结果:
You passed!
Your grade is C
例如:引用版本
#!/usr/bin/tclshset grade B;switch $grade {A {puts "Well done!"}B {puts "Excellent!"}C {puts "You passed!"}F {puts "Better try again"}default {puts "Invalid grade"}
}
puts "Your grade is $grade"
当上述代码被编译和执行时,它产生了以下结果:
Well done
Your grade is B
Tcl嵌套Switch语句
有可能有一个switch作为外开关的语句序列的一部分。即使在内外switch case 的常数包含共同的值,如果没有冲突将出现。
语法
嵌套switch语句的语法如下:
switch switchingString {matchString1 {body1switch switchingString {matchString1 {body1}matchString2 {body2}...matchStringn {bodyn}}}matchString2 {body2}
...matchStringn {bodyn}
}
示例
#!/usr/bin/tclshset a 100
set b 200switch $a {100 {puts "This is part of outer switch"switch $b {200 {puts "This is part of inner switch!"}}}
}
puts "Exact value of a is : $a"
puts "Exact value of a is : $b"
当上述代码被编译和执行时,它产生了以下结果:
This is part of outer switch
This is part of inner switch!
Exact value of a is : 100
Exact value of a is : 200
初识Tcl(四):Tcl 决策相关推荐
- 初识react(四) react中异步解决方案之 redux-saga
回顾 初识react(一) 揭开jsx语法和虚拟DOM面纱 初识react(二) 实现一个简版的html redux.js的demo 初识react(三)在 react中使用redux来实现简版计数器 ...
- 初识MIMO(四):MIMO的接收端检测技术及其仿真
初识MIMO(四):MIMO的接收端检测技术及其仿真 零 代码地址 https://github.com/liu-zongxi/MIMO_simulation 请大家看完觉得有用别忘了点赞收藏,git ...
- tcl mysql_MySQL·TCL语言
TCL语言就是我们所说的事务控制语言.首先事务的定义就是:一条或者多条SQL语句所组成的一个执行单位,且该组sql语句要么执行要么都不执行.事务有四大特性(ACID),分别为: (1).原子性(A): ...
- linux 安装tcl命令,TCL/TK Linux下安装 | 勤奋的小青蛙
原创文章,转载请注明: 转载自勤奋的小青蛙 本文链接地址: TCL/TK Linux下安装 在Linux下安装TCL/TK,可以有编译源代码的方式安装,也可以有直接通过二进制压缩包进行解压缩安装,本文 ...
- 初识Nginx四:nginx代理服务器配置缓存
文章目录 一.背景 二.配置 三.测试 四.注意点 一.背景 在上篇文章<初识Nginx二:配置一个反向代理服务器>中,我们成功通过Nginx的反向代理功能访问了上游的应用服务器. 在实际 ...
- tcl计算机语言,TCL语言
本词条缺少概述图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! Tcl (最早称为"工具命令语言""Tool Command Language", 但 ...
- python决策树 value_机器学习 | 算法笔记(四)- 决策树算法以及代码实现
概述 上一篇讲述了<机器学习 | 算法笔记(三)- 支持向量机算法以及代码实现>,本篇讲述机器学习算法决策树,内容包括模型介绍及代码实现. 决策树 决策树(Decision Tree)在机 ...
- FPGA初识:四选一多路选择器实例练习
一.项目创建 1.creat project2.确认项目名和项目位置,"√"确认生成子目录 3.生成RTL项目文件,且"√"不添加特殊源文件 4.根据上面的筛选 ...
- 初识Tcl(五):Tcl 循环
目录 Tcl循环 循环控制语句 无限循环 Tcl while循环 语法 流程图 示例 Tcl for循环 语法 流程图 示例 Tcl嵌套循环 语法 示例 Tcl break语句 语法 流程图 示例 T ...
- 初识Tcl(一):Tcl 命令
目录 第一个TCL程序 注释 标识符 Tcl空格 Tcl命令 命令替换 变量替换 反斜杠替换 TCL 是相当简单易学,让我们开始创建第一个Tcl程序! 第一个TCL程序 让我们写一个简单的Tcl程序. ...
最新文章
- Linux中shell的介绍
- SAP Spartacus 服务器端渲染找不到 index 视图的问题
- SVN学习(二)——SVN 提交、更新、解决冲突等操作步骤
- php 查询方法all,获取多条:all静态方法
- 执行perl xttdriver.pl报错Can't locate Getopt/Long.pm in @INC
- Oracle11g的安装和使用
- linux内核阅读感悟,读Kernel感悟-Linux内核启动-从hello world说起
- 微信二维码名片生成示例【转】
- 从数据库中导出数据库文档(新增了索引及表的描述信息)
- 如何制作和使用自签名证书
- cmd 复制文件夹内容
- win10 系统下CAD2009缩放卡顿问题解决
- 田英章硬笔书法24课 课堂练习
- Ubuntu-图形界面和字符界面切换快捷键
- Java序列化神器——Jprotobuf(小白篇)
- 谷底c语言,谷底线的基本画法是什么?
- 设计一个字符串子串删除函数
- 【小技巧】STA静态时序分析概述
- 单工通信、半双工通信和全双工通信的区别
- Electron在win7上加载plotyjs失败的解决方法
热门文章
- OpenERP 7.0版本发布
- 存储安全 系统的最后一道防线
- php定时删除目录,shell定时删除指定目录下的文件
- java 老年代回收_Java垃圾回收之老年代垃圾收集器
- 荣耀有鸿蒙手机吗,荣耀手机也能升鸿蒙!这5款机型用户有福了
- jndi weblogic mysql_Tomcat配置JNDI数据源连接池
- golang微服务框架对比_微服务里程碑,Golang与Spring Cloud Alibaba完美结合
- java 注册忘记密码操作_Java实战项目(1):swing图书管理系统的登录,注册,找回密码,增删查,个人解析等...
- 小米履带机器人充电时一直响_小米有品上线擦地机器人,每天放出去溜一圈,老婆夸我家务做的好...
- 聚类分析在用户行为中的实例_聚类分析在用户分类中的应用