Tcl之looping
1 While loop
while test body
The while
command evaluates test
as an expression. If test
is true, the code in body
is executed. After body
has been executed, test
is evaluated again.
Example:
set x 1 # This is a normal way to write a Tcl while loop. while {$x < 5} {
puts "x is $x"
set x [expr {$x + 1}]
} puts "exited first loop with X equal to $x\n" # The next example shows the difference between ".." and {...}
# How many times does the following loop run? Why does it not
# print on each pass? set x 0
while "$x < 5" {
set x [expr {$x + 1}]
if {$x > 7} break
if "$x > 3" continue
puts "x is $x"
} puts "exited second loop with X equal to $x"
2 For and incr
for start test next body
During evaluation of the for
command, the start
code is evaluated once, before any other arguments are evaluated. After the start code has been evaluated, the test
is evaluated. If the test
evaluates to true, then the body
is evaluated, and finally, the next
argument is evaluated. After evaluating the next
argument, the interpreter loops back to the test
, and repeats the process. If the test
evaluates as false, then the loop will exit immediately.
incr varName ?increment?
This command adds the value in the second argument to the variable named in the first argument. If no value is given for the second argument, it defaults to 1.
Example:
for {set i 0} {$i < 10} {incr i} {
puts "I inside first loop: $i"
} for {set i 3} {$i < 2} {incr i} {
puts "I inside second loop: $i"
} puts "Start"
set i 0
while {$i < 10} {
puts "I inside third loop: $i"
incr i
puts "I after incr: $i"
} set i 0
incr i
# This is equivalent to:
set i [expr {$i + 1}]
最新文章
- HDU 4833 Best Financing (DP)
- js中设置元素class的三种方法小结
- Java Executor并发框架(一)整体介绍
- docker的四种网络模式
- Redis与Memcached对比
- 从无到有开发连麦直播技术<;转>;
- 转:Linux中find命令-path -prune用法详解
- NodeJS中的http模块
- [JCIP笔记] (三)如何设计一个线程安全的对象
- [NOI2011]道路修建
- IFE第二天
- cadence16.6 暴力破解出现再次安装出现问题为Sever-----------问题
- 20190407 Word合并单元格
- jQueryValidate的表单提交ajax刷新代码
- 计算几何总结(Part 1~2)
- 网络管理命令ping和arping
- 小学四则运算APP 第一阶段冲刺 第二天-补
- 基于ngx_lua的动态服务路由方案
- C++ template —— 模板中的名称(三)
- sql_server角色成员身份权限