Debian Codegraph @

doc/for.n

raw
1 '\"
2 '\" Copyright (c) 1993 The Regents of the University of California.
3 '\" Copyright (c) 1994-1997 Sun Microsystems, Inc.
4 '\"
5 '\" See the file "license.terms" for information on usage and redistribution
6 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 '\"
8 .TH for n "" Tcl "Tcl Built-In Commands"
9 .so man.macros
10 .BS
11 '\" Note: do not modify the .SH NAME line immediately below!
12 .SH NAME
13 for \- 'For' loop
14 .SH SYNOPSIS
15 \fBfor \fIstart test next body\fR
16 .BE
17
18 .SH DESCRIPTION
19 .PP
20 \fBFor\fR is a looping command, similar in structure to the C
21 \fBfor\fR statement. The \fIstart\fR, \fInext\fR, and
22 \fIbody\fR arguments must be Tcl command strings, and \fItest\fR
23 is an expression string.
24 The \fBfor\fR command first invokes the Tcl interpreter to
25 execute \fIstart\fR. Then it repeatedly evaluates \fItest\fR as
26 an expression; if the result is non-zero it invokes the Tcl
27 interpreter on \fIbody\fR, then invokes the Tcl interpreter on \fInext\fR,
28 then repeats the loop. The command terminates when \fItest\fR evaluates
29 to 0. If a \fBcontinue\fR command is invoked within \fIbody\fR then
30 any remaining commands in the current execution of \fIbody\fR are skipped;
31 processing continues by invoking the Tcl interpreter on \fInext\fR, then
32 evaluating \fItest\fR, and so on. If a \fBbreak\fR command is invoked
33 within \fIbody\fR
34 or \fInext\fR,
35 then the \fBfor\fR command will
36 return immediately.
37 The operation of \fBbreak\fR and \fBcontinue\fR are similar to the
38 corresponding statements in C.
39 \fBFor\fR returns an empty string.
40 .PP
41 Note: \fItest\fR should almost always be enclosed in braces. If not,
42 variable substitutions will be made before the \fBfor\fR
43 command starts executing, which means that variable changes
44 made by the loop body will not be considered in the expression.
45 This is likely to result in an infinite loop. If \fItest\fR is
46 enclosed in braces, variable substitutions are delayed until the
47 expression is evaluated (before
48 each loop iteration), so changes in the variables will be visible.
49 See below for an example:
50 .SH EXAMPLES
51 .PP
52 Print a line for each of the integers from 0 to 9:
53 .PP
54 .CS
55 \fBfor\fR {set x 0} {$x<10} {incr x} {
56 puts "x is $x"
57 }
58 .CE
59 .PP
60 Either loop infinitely or not at all because the expression being
61 evaluated is actually the constant, or even generate an error! The
62 actual behaviour will depend on whether the variable \fIx\fR exists
63 before the \fBfor\fR command is run and whether its value is a value
64 that is less than or greater than/equal to ten, and this is because
65 the expression will be substituted before the \fBfor\fR command is
66 executed.
67 .PP
68 .CS
69 \fBfor\fR {set x 0} $x<10 {incr x} {
70 puts "x is $x"
71 }
72 .CE
73 .PP
74 Print out the powers of two from 1 to 1024:
75 .PP
76 .CS
77 \fBfor\fR {set x 1} {$x<=1024} {set x [expr {$x * 2}]} {
78 puts "x is $x"
79 }
80 .CE
81 .SH "SEE ALSO"
82 break(n), continue(n), foreach(n), while(n)
83 .SH KEYWORDS
84 boolean, for, iteration, loop
85 '\" Local Variables:
86 '\" mode: nroff
87 '\" End: