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}]
  • 相关阅读:
    操作集锦
    大吉大利(算贡献)
    96. 奇怪的汉诺塔
    P760通天之汉诺塔
    First Last Sorting
    9.12小结
    day_06
    day_05作业
    day_05
    day_04
  • 原文地址:https://www.cnblogs.com/mengdie/p/4625701.html
Copyright © 2011-2022 走看看