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