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

SEE ALSO

61       break, continue, foreach, while
62
63

KEYWORDS

65       for, iteration, looping
66
67
68
69Tcl                                                                     for(n)
Impressum