§12 循环101-while循环

                        §12   循环101-while循环

            While和for具有一定的可替换性。语法如下:

  • while test body

continue终止当次循环,break退出整个循环。

            注意while之后要用大括号来括住,用引号的话会导致只进行一次替换,导致无限循环。

实例:

set x 1

# This is a normal way to write a Tcl while loop.

while {$x < 5} {

    puts "x is $x"

    set x [expr {$x + 1}]

}

puts "exited first loop with X equal to $x "

# The next example shows the difference between ".." and {...}

# How many times does the following loop run?  Why does it not

# print on each pass?

set x 0

while "$x < 5" {

    set x [expr {$x + 1}]

    if {$x > 7} break

    if "$x > 3" continue

    puts "x is $x"

}

puts "exited second loop with X equal to $x"

执行结果:

x is 1

x is 2

x is 3

x is 4

exited first loop with X equal to 5

 

x is 1

x is 2

x is 3

exited second loop with X equal to 8

存在问题:while为什么要用大括号括住 的解释还没有明白。

原文地址:https://www.cnblogs.com/hfyfpga/p/4255176.html