1for(n)                       Tcl Built-In Commands                      for(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       for - 'For' loop
9

SYNOPSIS

11       for start test next body
12______________________________________________________________________________
13
14

DESCRIPTION

16       For  is a looping command, similar in structure to the C for statement.
17       The start, next, and body arguments must be Tcl  command  strings,  and
18       test  is  an  expression string.  The for command first invokes the Tcl
19       interpreter to execute start.  Then it repeatedly evaluates test as  an
20       expression; if the result is non-zero it invokes the Tcl interpreter on
21       body, then invokes the Tcl interpreter on next, then repeats the  loop.
22       The command terminates when test evaluates to 0.  If a continue command
23       is invoked within body then any remaining commands in the current  exe‐
24       cution  of  body  are skipped; processing continues by invoking the Tcl
25       interpreter on next, then evaluating test, and so on.  If a break  com‐
26       mand  is  invoked within body or next, then the for command will return
27       immediately.  The operation of break and continue are  similar  to  the
28       corresponding statements in C.  For returns an empty string.
29
30       Note:  test  should almost always be enclosed in braces.  If not, vari‐
31       able substitutions will be made before the for command  starts  execut‐
32       ing,  which  means that variable changes made by the loop body will not
33       be considered in the expression.  This is likely to result in an  infi‐
34       nite  loop.   If test is enclosed in braces, variable substitutions are
35       delayed until the expression is evaluated (before each loop iteration),
36       so changes in the variables will be visible.  See below for an example:
37

EXAMPLES

39       Print a line for each of the integers from 0 to 9:
40
41              for {set x 0} {$x<10} {incr x} {
42                  puts "x is $x"
43              }
44
45       Either loop infinitely or not at all because the expression being eval‐
46       uated is actually the constant, or even generate an error!  The  actual
47       behaviour  will  depend on whether the variable x exists before the for
48       command is run and whether its value is a value that is  less  than  or
49       greater  than/equal  to ten, and this is because the expression will be
50       substituted before the for command is executed.
51
52              for {set x 0} $x<10 {incr x} {
53                  puts "x is $x"
54              }
55
56       Print out the powers of two from 1 to 1024:
57
58              for {set x 1} {$x<=1024} {set x [expr {$x * 2}]} {
59                  puts "x is $x"
60              }
61

SEE ALSO

63       break(n), continue(n), foreach(n), while(n)
64

KEYWORDS

66       boolean, for, iteration, loop
67
68
69
70Tcl                                                                     for(n)
Impressum