50 lines
2.7 KiB
HTML
50 lines
2.7 KiB
HTML
<HTML><HEAD><TITLE>Built-In Commands - for manual page</TITLE></HEAD><BODY>
|
|
<H3><A NAME="M2">NAME</A></H3>
|
|
for - ``For'' loop
|
|
<H3><A NAME="M3">SYNOPSIS</A></H3>
|
|
<B>for </B><I>start test next body</I><BR>
|
|
<H3><A NAME="M4">DESCRIPTION</A></H3>
|
|
<B>For</B> is a looping command, similar in structure to the C
|
|
<B>for</B> statement. The <I>start</I>, <I>next</I>, and
|
|
<I>body</I> arguments must be Tcl command strings, and <I>test</I>
|
|
is an expression string.
|
|
The <B>for</B> command first invokes the Tcl interpreter to
|
|
execute <I>start</I>. Then it repeatedly evaluates <I>test</I> as
|
|
an expression; if the result is non-zero it invokes the Tcl
|
|
interpreter on <I>body</I>, then invokes the Tcl interpreter on <I>next</I>,
|
|
then repeats the loop. The command terminates when <I>test</I> evaluates
|
|
to 0. If a <B><A HREF="../TkCmd/continue.htm">continue</A></B> command is invoked within <I>body</I> then
|
|
any remaining commands in the current execution of <I>body</I> are skipped;
|
|
processing continues by invoking the Tcl interpreter on <I>next</I>, then
|
|
evaluating <I>test</I>, and so on. If a <B><A HREF="../TkCmd/break.htm">break</A></B> command is invoked
|
|
within <I>body</I>
|
|
or <I>next</I>,
|
|
then the <B>for</B> command will
|
|
return immediately.
|
|
The operation of <B><A HREF="../TkCmd/break.htm">break</A></B> and <B><A HREF="../TkCmd/continue.htm">continue</A></B> are similar to the
|
|
corresponding statements in C.
|
|
<B>For</B> 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>for</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<10</B>:
|
|
<PRE>for {set x 0} {$x<10} {incr x} {
|
|
puts "x is $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/foreach.htm">foreach</A></B>, <B><A HREF="../TkCmd/while.htm">while</A></B>
|
|
<H3><A NAME="M6">KEYWORDS</A></H3>
|
|
<A href="../Keywords/F.htm#for">for</A>, <A href="../Keywords/I.htm#iteration">iteration</A>, <A href="../Keywords/L.htm#looping">looping</A>
|
|
<HR><PRE>
|
|
<A HREF="../copyright.htm">Copyright</A> © 1993 The Regents of the University of California.
|
|
<A HREF="../copyright.htm">Copyright</A> © 1994-1997 Sun Microsystems, Inc.
|
|
<A HREF="../copyright.htm">Copyright</A> © 1995-1997 Roger E. Critchlow Jr.</PRE>
|
|
</BODY></HTML>
|