projman/hlp/en/tcl/while.htm
2015-10-19 14:27:31 +04:00

45 lines
2.4 KiB
HTML

<HTML><HEAD><TITLE>Built-In Commands - while manual page</TITLE></HEAD><BODY>
<H3><A NAME="M2">NAME</A></H3>
while - Execute script repeatedly as long as a condition is met
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>while </B><I>test body</I><BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
The <B>while</B> command evaluates <I>test</I> as an expression
(in the same way that <B><A HREF="../TkCmd/expr.htm">expr</A></B> evaluates its argument).
The value of the expression must a proper boolean
value; if it is a true value
then <I>body</I> is executed by passing it to the Tcl interpreter.
Once <I>body</I> has been executed then <I>test</I> is evaluated
again, and the process repeats until eventually <I>test</I>
evaluates to a false boolean value. <B><A HREF="../TkCmd/continue.htm">Continue</A></B>
commands may be executed inside <I>body</I> to terminate the current
iteration of the loop, and <B><A HREF="../TkCmd/break.htm">break</A></B>
commands may be executed inside <I>body</I> to cause immediate
termination of the <B>while</B> command. The <B>while</B> command
always returns an empty string.
<P>
Note: <I>test</I> should almost always be enclosed in braces. If not,
variable substitutions will be made before the <B>while</B>
command starts executing, which means that variable changes
made by the loop body will not be considered in the expression.
This is likely to result in an infinite loop. If <I>test</I> is
enclosed in braces, variable substitutions are delayed until the
expression is evaluated (before
each loop iteration), so changes in the variables will be visible.
For an example, try the following script with and without the braces
around <B>$x&lt;10</B>:
<PRE>set x 0
while {$x&lt;10} {
puts &quot;x is $x&quot;
incr x
}</PRE>
<H3><A NAME="M5">SEE ALSO</A></H3>
<B><A HREF="../TkCmd/break.htm">break</A></B>, <B><A HREF="../TkCmd/continue.htm">continue</A></B>, <B><A HREF="../TkCmd/for.htm">for</A></B>, <B><A HREF="../TkCmd/foreach.htm">foreach</A></B>
<H3><A NAME="M6">KEYWORDS</A></H3>
<A href="../Keywords/B.htm#boolean value">boolean value</A>, <A href="../Keywords/L.htm#loop">loop</A>, <A href="../Keywords/T.htm#test">test</A>, <A href="../Keywords/W.htm#while">while</A>
<HR><PRE>
<A HREF="../copyright.htm">Copyright</A> &#169; 1993 The Regents of the University of California.
<A HREF="../copyright.htm">Copyright</A> &#169; 1994-1997 Sun Microsystems, Inc.
<A HREF="../copyright.htm">Copyright</A> &#169; 1995-1997 Roger E. Critchlow Jr.</PRE>
</BODY></HTML>