[25] | 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 |
---|
| 15 | for \- '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 |
---|
| 25 | is an expression string. |
---|
| 26 | The \fBfor\fR command first invokes the Tcl interpreter to |
---|
| 27 | execute \fIstart\fR. Then it repeatedly evaluates \fItest\fR as |
---|
| 28 | an expression; if the result is non-zero it invokes the Tcl |
---|
| 29 | interpreter on \fIbody\fR, then invokes the Tcl interpreter on \fInext\fR, |
---|
| 30 | then repeats the loop. The command terminates when \fItest\fR evaluates |
---|
| 31 | to 0. If a \fBcontinue\fR command is invoked within \fIbody\fR then |
---|
| 32 | any remaining commands in the current execution of \fIbody\fR are skipped; |
---|
| 33 | processing continues by invoking the Tcl interpreter on \fInext\fR, then |
---|
| 34 | evaluating \fItest\fR, and so on. If a \fBbreak\fR command is invoked |
---|
| 35 | within \fIbody\fR |
---|
| 36 | or \fInext\fR, |
---|
| 37 | then the \fBfor\fR command will |
---|
| 38 | return immediately. |
---|
| 39 | The operation of \fBbreak\fR and \fBcontinue\fR are similar to the |
---|
| 40 | corresponding statements in C. |
---|
| 41 | \fBFor\fR returns an empty string. |
---|
| 42 | .PP |
---|
| 43 | Note: \fItest\fR should almost always be enclosed in braces. If not, |
---|
| 44 | variable substitutions will be made before the \fBfor\fR |
---|
| 45 | command starts executing, which means that variable changes |
---|
| 46 | made by the loop body will not be considered in the expression. |
---|
| 47 | This is likely to result in an infinite loop. If \fItest\fR is |
---|
| 48 | enclosed in braces, variable substitutions are delayed until the |
---|
| 49 | expression is evaluated (before |
---|
| 50 | each loop iteration), so changes in the variables will be visible. |
---|
| 51 | See below for an example: |
---|
| 52 | .SH EXAMPLES |
---|
| 53 | Print a line for each of the integers from 0 to 10: |
---|
| 54 | .CS |
---|
| 55 | for {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 | .CS |
---|
| 68 | for {set x 0} $x<10 {incr x} { |
---|
| 69 | puts "x is $x" |
---|
| 70 | } |
---|
| 71 | .CE |
---|
| 72 | .PP |
---|
| 73 | Print out the powers of two from 1 to 1024: |
---|
| 74 | .CS |
---|
| 75 | for {set x 1} {$x<=1024} {set x [expr {$x * 2}]} { |
---|
| 76 | puts "x is $x" |
---|
| 77 | } |
---|
| 78 | .CE |
---|
| 79 | |
---|
| 80 | .SH "SEE ALSO" |
---|
| 81 | break, continue, foreach, while |
---|
| 82 | |
---|
| 83 | .SH KEYWORDS |
---|
| 84 | for, iteration, looping |
---|