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}]
  • 相关阅读:
    JAVA学习---JAVA基础元素
    JAVA开发-eclipse打开失败:A Java Runtime Environment(JRE) or Java Development Kit(JDK) must be available……的解决办法
    文章转载---西工大博士生文言文答辩致谢
    python中正则匹配之re模块
    python中random模块的使用
    python中函数的定义及调用
    Python中字符编码及转码
    python中字符串输出格式
    数据库与表的剩余操作
    线程其他相关用法和守护线程
  • 原文地址:https://www.cnblogs.com/mengdie/p/4625701.html
Copyright © 2011-2022 走看看