• 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}]
  • 相关阅读:
    编译安装libusb操作流水账
    SM总线控制器没有驱动 关机后又自动重启
    mtd-utils编译安装过程
    STM32开发/烧录/调试环境搭建 基于:Win10+STM32Cube+openocd+cmsis-dap(dap-link)
    EchoServer和EchoClient模型的改进1之多线程
    CountDownLatch分析
    linux-java环境安装以及ssh
    ServerSocket01
    使用SecureCRT工具部署项目
    javaScript笔记01
  • 原文地址:https://www.cnblogs.com/mengdie/p/4625701.html
Copyright © 2020-2023  润新知