From 4149000e58b323d2eab144b32acfa9f62e8576f1 Mon Sep 17 00:00:00 2001 From: dkf Date: Wed, 5 May 2004 22:08:08 +0000 Subject: More examples. --- ChangeLog | 4 ++++ doc/for.n | 29 +++++++++++++++++++++++++---- doc/while.n | 11 ++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index efaac16..2616d0e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2004-05-05 Donal K. Fellows + + * doc/for.n, doc/while.n: More examples. + 2004-05-05 Don Porter * tests/unixInit.test (unixInit-2.10): Test correction for Mac OSX. diff --git a/doc/for.n b/doc/for.n index 5cf267c..2b98453 100644 --- a/doc/for.n +++ b/doc/for.n @@ -5,7 +5,7 @@ '\" See the file "license.terms" for information on usage and redistribution '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. '\" -'\" RCS: @(#) $Id: for.n,v 1.3 2000/09/07 14:27:48 poenitz Exp $ +'\" RCS: @(#) $Id: for.n,v 1.4 2004/05/05 22:08:10 dkf Exp $ '\" .so man.macros .TH for n "" Tcl "Tcl Built-In Commands" @@ -48,11 +48,32 @@ This is likely to result in an infinite loop. If \fItest\fR is enclosed in braces, variable substitutions are delayed until the expression is evaluated (before each loop iteration), so changes in the variables will be visible. -For an example, try the following script with and without the braces -around \fB$x<10\fR: +See below for an example: +.SH EXAMPLES +Print a line for each of the integers from 0 to 10: .CS for {set x 0} {$x<10} {incr x} { - puts "x is $x" + puts "x is $x" +} +.CE + +Either loop infinitely or not at all because the expression being +evaluated is actually the constant, or even generate an error! The +actual behaviour will depend on whether the variable \fIx\fR exists +before the \fBfor\fR command is run and whether its value is a value +that is less than or greater than/equal to ten, and this is because +the expression will be substituted before the \fBfor\fR command is +executed. +.CS +for {set x 0} $x<10 {incr x} { + puts "x is $x" +} +.CE + +Print out the powers of two from 1 to 1024: +.CS +for {set x 1} {$x<=1024} {set x [expr {$x * 2}]} { + puts "x is $x" } .CE diff --git a/doc/while.n b/doc/while.n index ba5a584..e1c2ec3 100644 --- a/doc/while.n +++ b/doc/while.n @@ -5,7 +5,7 @@ '\" See the file "license.terms" for information on usage and redistribution '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. '\" -'\" RCS: @(#) $Id: while.n,v 1.3 2000/09/07 14:27:52 poenitz Exp $ +'\" RCS: @(#) $Id: while.n,v 1.4 2004/05/05 22:08:10 dkf Exp $ '\" .so man.macros .TH while n "" Tcl "Tcl Built-In Commands" @@ -50,6 +50,15 @@ while {$x<10} { incr x } .CE +.SH EXAMPLE +Read lines from a channel until we get to the end of the stream, and +print them out with a line-number prepended: +.CS +set lineCount 0 +while {[gets $chan line] >= 0} { + puts "[incr lineCount]: $line" +} +.CE .SH "SEE ALSO" break(n), continue(n), for(n), foreach(n) -- cgit v0.12