| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH while n "" Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
while \- Execute script repeatedly as long as a condition is met |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBwhile \fItest body\fR |
| 16 |
.BE |
| 17 |
.SH DESCRIPTION |
| 18 |
.PP |
| 19 |
The \fBwhile\fR command evaluates \fItest\fR as an expression |
| 20 |
(in the same way that \fBexpr\fR evaluates its argument). |
| 21 |
The value of the expression must a proper boolean |
| 22 |
value; if it is a true value |
| 23 |
then \fIbody\fR is executed by passing it to the Tcl interpreter. |
| 24 |
Once \fIbody\fR has been executed then \fItest\fR is evaluated |
| 25 |
again, and the process repeats until eventually \fItest\fR |
| 26 |
evaluates to a false boolean value. \fBContinue\fR |
| 27 |
commands may be executed inside \fIbody\fR to terminate the current |
| 28 |
iteration of the loop, and \fBbreak\fR |
| 29 |
commands may be executed inside \fIbody\fR to cause immediate |
| 30 |
termination of the \fBwhile\fR command. The \fBwhile\fR command |
| 31 |
always returns an empty string. |
| 32 |
.PP |
| 33 |
Note: \fItest\fR should almost always be enclosed in braces. If not, |
| 34 |
variable substitutions will be made before the \fBwhile\fR |
| 35 |
command starts executing, which means that variable changes |
| 36 |
made by the loop body will not be considered in the expression. |
| 37 |
This is likely to result in an infinite loop. If \fItest\fR is |
| 38 |
enclosed in braces, variable substitutions are delayed until the |
| 39 |
expression is evaluated (before |
| 40 |
each loop iteration), so changes in the variables will be visible. |
| 41 |
For an example, try the following script with and without the braces |
| 42 |
around \fB$x<10\fR: |
| 43 |
.PP |
| 44 |
.CS |
| 45 |
set x 0 |
| 46 |
\fBwhile\fR {$x<10} { |
| 47 |
puts "x is $x" |
| 48 |
incr x |
| 49 |
} |
| 50 |
.CE |
| 51 |
.SH EXAMPLE |
| 52 |
.PP |
| 53 |
Read lines from a channel until we get to the end of the stream, and |
| 54 |
print them out with a line-number prepended: |
| 55 |
.PP |
| 56 |
.CS |
| 57 |
set lineCount 0 |
| 58 |
\fBwhile\fR {[gets $chan line] >= 0} { |
| 59 |
puts "[incr lineCount]: $line" |
| 60 |
} |
| 61 |
.CE |
| 62 |
.SH "SEE ALSO" |
| 63 |
break(n), continue(n), for(n), foreach(n) |
| 64 |
.SH KEYWORDS |
| 65 |
boolean, loop, test, while |