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}]
  • 相关阅读:
    快速搭建一个本地的FTP服务器
    Node.js安装及环境配置之Windows篇
    在win10上安装oracle10g
    win10安装oracle11g客户端
    解决:Java source1.6不支持diamond运算符,请使用source 7或更高版本以启用diamond运算符
    idea 右侧 无 meven 菜单
    idea导入maven项目不能识别pom.xml文件解决办法
    PostgresSQL客户端pgAdmin4使用
    PostgreSQL 创建数据库
    PostgreSQL 数据类型
  • 原文地址:https://www.cnblogs.com/mengdie/p/4625701.html
Copyright © 2011-2022 走看看