Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/for.n @ 37

Last change on this file since 37 was 25, checked in by landauf, 16 years ago

added tcl to libs

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