1while(n) Tcl Built-In Commands while(n)
2
3
4
5______________________________________________________________________________
6
8 while - Execute script repeatedly as long as a condition is met
9
11 while test body
12_________________________________________________________________
13
14
16 The while command evaluates test as an expression (in the same way that
17 expr evaluates its argument). The value of the expression must a
18 proper boolean value; if it is a true value then body is executed by
19 passing it to the Tcl interpreter. Once body has been executed then
20 test is evaluated again, and the process repeats until eventually test
21 evaluates to a false boolean value. Continue commands may be executed
22 inside body to terminate the current iteration of the loop, and break
23 commands may be executed inside body to cause immediate termination of
24 the while command. The while command always returns an empty string.
25
26 Note: test should almost always be enclosed in braces. If not, vari‐
27 able substitutions will be made before the while command starts execut‐
28 ing, which means that variable changes made by the loop body will not
29 be considered in the expression. This is likely to result in an infi‐
30 nite loop. If test is enclosed in braces, variable substitutions are
31 delayed until the expression is evaluated (before each loop iteration),
32 so changes in the variables will be visible. For an example, try the
33 following script with and without the braces around $x<10:
34 set x 0
35 while {$x<10} {
36 puts "x is $x"
37 incr x
38 }
39
41 Read lines from a channel until we get to the end of the stream, and
42 print them out with a line-number prepended:
43 set lineCount 0
44 while {[gets $chan line] >= 0} {
45 puts "[incr lineCount]: $line"
46 }
47
48
50 break(n), continue(n), for(n), foreach(n)
51
52
54 boolean value, loop, test, while
55
56
57
58Tcl while(n)