zoukankan      html  css  js  c++  java
  • 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:

     1 set x 1
     2 
     3 # This is a normal way to write a Tcl while loop.
     4 
     5 while {$x < 5} {
     6     puts "x is $x"
     7     set x [expr {$x + 1}]
     8 }
     9 
    10 puts "exited first loop with X equal to $x
    "
    11 
    12 # The next example shows the difference between ".." and {...}
    13 # How many times does the following loop run?  Why does it not
    14 # print on each pass?
    15 
    16 set x 0
    17 while "$x < 5" {
    18     set x [expr {$x + 1}]
    19     if {$x > 7} break
    20     if "$x > 3" continue
    21     puts "x is $x"
    22 }
    23 
    24 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:

     1 for {set i 0} {$i < 10} {incr i} {
     2     puts "I inside first loop: $i"
     3 }
     4 
     5 for {set i 3} {$i < 2} {incr i} {
     6     puts "I inside second loop: $i"
     7 }
     8 
     9 puts "Start"
    10 set i 0
    11 while {$i < 10} {
    12     puts "I inside third loop: $i"
    13     incr i
    14     puts "I after incr: $i"
    15 }
    16 
    17 set i 0
    18 incr i
    19 # This is equivalent to:
    20 set i [expr {$i + 1}]
  • 相关阅读:
    V2EX 上收藏Top200
    在heroku上部署gost代理服务端
    nano编辑器使用教程
    Amazon EC2免费VPS防止超额被扣钱三大方法:流量 硬盘读写 运行时长
    Go语言开发环境配置
    HTML5 and CSS3 开发
    使用 Eclipse PhoneGap 构建 Android 应用程序入门
    脚本之家
    CSS网页布局全精通
    使用面向 iOS 的本机插件扩展 PhoneGap
  • 原文地址:https://www.cnblogs.com/mengdie/p/4625701.html
Copyright © 2011-2022 走看看