1mNAME0m
       for - ``For'' loop

1mSYNOPSIS0m
       1mfor 4m22mstart24m 4mtest24m 4mnext24m 4mbody0m


1mDESCRIPTION0m
       1mFor  22mis a looping command, similar in structure to the C 1mfor 22mstatement.
       The 4mstart24m, 4mnext24m, and 4mbody24m arguments must be Tcl  command  strings,  and
       4mtest24m  is  an  expression string.  The 1mfor 22mcommand first invokes the Tcl
       interpreter to execute 4mstart24m.  Then it repeatedly evaluates 4mtest24m as  an
       expression; if the result is non-zero it invokes the Tcl interpreter on
       4mbody24m, then invokes the Tcl interpreter on 4mnext24m, then repeats the  loop.
       The command terminates when 4mtest24m evaluates to 0.  If a 1mcontinue 22mcommand
       is invoked within 4mbody24m then any remaining commands in the current  exe-
       cution  of  4mbody24m  are skipped; processing continues by invoking the Tcl
       interpreter on 4mnext24m, then evaluating 4mtest24m, and so on.  If a 1mbreak  22mcom-
       mand  is  invoked within 4mbody24m or 4mnext24m, then the 1mfor 22mcommand will return
       immediately.  The operation of 1mbreak 22mand 1mcontinue 22mare  similar  to  the
       corresponding statements in C.  1mFor 22mreturns an empty string.

       Note:  4mtest24m  should almost always be enclosed in braces.  If not, vari-
       able substitutions will be made before the 1mfor 22mcommand  starts  execut-
       ing,  which  means that variable changes made by the loop body will not
       be considered in the expression.  This is likely to result in an  infi-
       nite  loop.   If 4mtest24m 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.  See below for an example:

1mEXAMPLES0m
       Print a line for each of the integers from 0 to 10:
              for {set x 0} {$x<10} {incr x} {
                 puts "x is $x"
              }

       Either loop infinitely or not at all because the expression being eval-
       uated  is actually the constant, or even generate an error!  The actual
       behaviour will depend on whether the variable 4mx24m exists before  the  1mfor0m
       command  is  run  and whether its value is a value that is less than or
       greater than/equal to ten, and this is because the expression  will  be
       substituted before the 1mfor 22mcommand is executed.
              for {set x 0} $x<10 {incr x} {
                 puts "x is $x"
              }

       Print out the powers of two from 1 to 1024:
              for {set x 1} {$x<=1024} {set x [expr {$x * 2}]} {
                 puts "x is $x"
              }


1mSEE ALSO0m
       break, continue, foreach, while


1mKEYWORDS0m
