summaryrefslogtreecommitdiffstats
path: root/doc/while.n
blob: dcaf8fcc74428a013b88fe739ac78cae2eab623d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'\"
'\" Copyright (c) 1993 The Regents of the University of California.
'\" Copyright (c) 1994-1997 Sun Microsystems, Inc.
'\"
'\" 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.18.1 2004/10/27 14:43:15 dkf Exp $
'\" 
.so man.macros
.TH while n "" Tcl "Tcl Built-In Commands"
.BS
'\" Note:  do not modify the .SH NAME line immediately below!
.SH NAME
while \- Execute script repeatedly as long as a condition is met
.SH SYNOPSIS
\fBwhile \fItest body\fR
.BE

.SH DESCRIPTION
.PP
The \fBwhile\fR command evaluates \fItest\fR as an expression
(in the same way that \fBexpr\fR evaluates its argument).
The value of the expression must a proper boolean
value; if it is a true value
then \fIbody\fR is executed by passing it to the Tcl interpreter.
Once \fIbody\fR has been executed then \fItest\fR is evaluated
again, and the process repeats until eventually \fItest\fR
evaluates to a false boolean value.  \fBContinue\fR
commands may be executed inside \fIbody\fR to terminate the current
iteration of the loop, and \fBbreak\fR
commands may be executed inside \fIbody\fR to cause immediate
termination of the \fBwhile\fR command.  The \fBwhile\fR command
always returns an empty string.
.PP
Note: \fItest\fR should almost always be enclosed in braces.  If not,
variable substitutions will be made before the \fBwhile\fR
command starts executing, which means that variable changes
made by the loop body will not be considered in the expression.
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:
.CS
set x 0
\fBwhile\fR {$x<10} {
    puts "x is $x"
    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
\fBwhile\fR {[gets $chan line] >= 0} {
    puts "[incr lineCount]: $line"
}
.CE

.SH "SEE ALSO"
break(n), continue(n), for(n), foreach(n)

.SH KEYWORDS
boolean value, loop, test, while
040000 VT0 = 0 VT1 = 0040000 FFDLY = 0100000 FF0 = 0 FF1 = 0100000 PAGEOUT = 0200000 WRAP = 0400000 CBAUD = 000000017 CSIZE = 000000060 CS5 = 0 CS6 = 000000020 CS7 = 000000040 CS8 = 000000060 CSTOPB = 000000100 CREAD = 000000200 PARENB = 000000400 PARODD = 000001000 HUPCL = 000002000 CLOCAL = 000004000 RCV1EN = 000010000 XMT1EN = 000020000 LOBLK = 000040000 XCLUDE = 000100000 CIBAUD = 003600000 PAREXT = 004000000 CNEW_RTSCTS = 010000000 ISIG = 0000001 ICANON = 0000002 XCASE = 0000004 ECHO = 0000010 ECHOE = 0000020 ECHOK = 0000040 ECHONL = 0000100 NOFLSH = 0000200 IEXTEN = 0000400 ITOSTOP = 0100000 TOSTOP = ITOSTOP ECHOCTL = 0001000 ECHOPRT = 0002000 ECHOKE = 0004000 DEFECHO = 0010000 FLUSHO = 0020000 PENDIN = 0040000 TIOC = (ord('T')<<8) TCGETA = (TIOC|1) TCSETA = (TIOC|2) TCSETAW = (TIOC|3) TCSETAF = (TIOC|4) TCSBRK = (TIOC|5) TCXONC = (TIOC|6) TCFLSH = (TIOC|7) LDISC0 = 0 LDISC1 = 1 NTTYDISC = LDISC1 TIOCFLUSH = (TIOC|12) TCSETLABEL = (TIOC|31) TCDSET = (TIOC|32) TCBLKMD = (TIOC|33) TIOCPKT = (TIOC|112) TIOCPKT_DATA = 0x00 TIOCPKT_FLUSHREAD = 0x01 TIOCPKT_FLUSHWRITE = 0x02 TIOCPKT_NOSTOP = 0x10 TIOCPKT_DOSTOP = 0x20 TIOCPKT_IOCTL = 0x40 TIOCNOTTY = (TIOC|113) TIOCSTI = (TIOC|114) TFIOC = (ord('F')<<8) oFIONREAD = (TFIOC|127) TO_STOP = LOBLK IOCTYPE = 0xff00 TCGETS = (TIOC|13) TCSETS = (TIOC|14) TCSETSW = (TIOC|15) TCSETSF = (TIOC|16) TCSANOW = ((ord('T')<<8)|14) TCSADRAIN = ((ord('T')<<8)|15) TCSAFLUSH = ((ord('T')<<8)|16) TCIFLUSH = 0 TCOFLUSH = 1 TCIOFLUSH = 2 TCOOFF = 0 TCOON = 1 TCIOFF = 2 TCION = 3 tIOC = (ord('t')<<8) TIOCGETD = (tIOC|0) TIOCSETD = (tIOC|1) TIOCHPCL = (tIOC|2) TIOCGETP = (tIOC|8) TIOCSETP = (tIOC|9) TIOCSETN = (tIOC|10) TIOCEXCL = (tIOC|13) TIOCNXCL = (tIOC|14) TIOCSETC = (tIOC|17) TIOCGETC = (tIOC|18) TIOCLBIS = (tIOC|127) TIOCLBIC = (tIOC|126) TIOCLSET = (tIOC|125) TIOCLGET = (tIOC|124) TIOCSBRK = (tIOC|123) TIOCCBRK = (tIOC|122) TIOCSDTR = (tIOC|121) TIOCCDTR = (tIOC|120) TIOCSLTC = (tIOC|117) TIOCGLTC = (tIOC|116) TIOCOUTQ = (tIOC|115) TIOCSTOP = (tIOC|111) TIOCSTART = (tIOC|110) TIOCGSID = (tIOC|22) TIOCSSID = (tIOC|24) TIOCMSET = (tIOC|26) TIOCMBIS = (tIOC|27) TIOCMBIC = (tIOC|28) TIOCMGET = (tIOC|29) TIOCM_LE = 0001 TIOCM_DTR = 0002 TIOCM_RTS = 0004 TIOCM_ST = 0010 TIOCM_SR = 0020 TIOCM_CTS = 0040 TIOCM_CAR = 0100 TIOCM_CD = TIOCM_CAR TIOCM_RNG = 0200 TIOCM_RI = TIOCM_RNG TIOCM_DSR = 0400 TIOCREMOTE = (tIOC|30) TIOCSIGNAL = (tIOC|31) ISPTM = ((ord('P')<<8)|1) UNLKPT = ((ord('P')<<8)|2) SVR4SOPEN = ((ord('P')<<8)|100) LDIOC = (ord('D')<<8) LDOPEN = (LDIOC|0) LDCLOSE = (LDIOC|1) LDCHG = (LDIOC|2) LDGETT = (LDIOC|8) LDSETT = (LDIOC|9) LDSMAP = (LDIOC|10) LDGMAP = (LDIOC|11) LDNMAP = (LDIOC|12) DIOC = (ord('d')<<8) DIOCGETP = (DIOC|8) DIOCSETP = (DIOC|9) FIORDCHK = ((ord('f')<<8)|3) CLNEXT = CTRL(ord('v')) CWERASE = CTRL(ord('w')) CFLUSHO = CTRL(ord('o')) CFLUSH = CFLUSHO CRPRNT = CTRL(ord('r')) CDSUSP = CTRL(ord('y')) SSPEED = B9600 TERM_NONE = 0 TERM_TEC = 1 TERM_V61 = 2 TERM_V10 = 3 TERM_TEX = 4 TERM_D40 = 5 TERM_H45 = 6 TERM_D42 = 7 TM_NONE = 0000 TM_SNL = 0001 TM_ANL = 0002 TM_LCF = 0004 TM_CECHO = 0010 TM_CINVIS = 0020 TM_SET = 0200 LDISC0 = 0 LDISC1 = 1 NTTYDISC = LDISC1 TIOCFLUSH = (TIOC|12) TCSETLABEL = (TIOC|31) TCDSET = (TIOC|32) TCBLKMD = (TIOC|33) TIOCPKT = (TIOC|112) TIOCPKT_DATA = 0x00 TIOCPKT_FLUSHREAD = 0x01 TIOCPKT_FLUSHWRITE = 0x02 TIOCPKT_NOSTOP = 0x10 TIOCPKT_DOSTOP = 0x20 TIOCPKT_IOCTL = 0x40 TIOCNOTTY = (TIOC|113) TIOCSTI = (TIOC|114) TFIOC = (ord('F')<<8) oFIONREAD = (TFIOC|127) TO_STOP = LOBLK IOCTYPE = 0xff00 TCGETS = (TIOC|13) TCSETS = (TIOC|14) TCSETSW = (TIOC|15) TCSETSF = (TIOC|16) TCSANOW = ((ord('T')<<8)|14) TCSADRAIN = ((ord('T')<<8)|15) TCSAFLUSH = ((ord('T')<<8)|16) TCIFLUSH = 0 TCOFLUSH = 1 TCIOFLUSH = 2 TCOOFF = 0 TCOON = 1 TCIOFF = 2 TCION = 3 tIOC = (ord('t')<<8) TIOCGETD = (tIOC|0) TIOCSETD = (tIOC|1) TIOCHPCL = (tIOC|2) TIOCGETP = (tIOC|8) TIOCSETP = (tIOC|9) TIOCSETN = (tIOC|10) TIOCEXCL = (tIOC|13) TIOCNXCL = (tIOC|14) TIOCSETC = (tIOC|17) TIOCGETC = (tIOC|18) TIOCLBIS = (tIOC|127) TIOCLBIC = (tIOC|126) TIOCLSET = (tIOC|125) TIOCLGET = (tIOC|124) TIOCSBRK = (tIOC|123) TIOCCBRK = (tIOC|122) TIOCSDTR = (tIOC|121) TIOCCDTR = (tIOC|120) TIOCSLTC = (tIOC|117) TIOCGLTC = (tIOC|116) TIOCOUTQ = (tIOC|115) TIOCSTOP = (tIOC|111) TIOCSTART = (tIOC|110) TIOCGSID = (tIOC|22) TIOCSSID = (tIOC|24) TIOCMSET = (tIOC|26) TIOCMBIS = (tIOC|27) TIOCMBIC = (tIOC|28) TIOCMGET = (tIOC|29) TIOCM_LE = 0001 TIOCM_DTR = 0002 TIOCM_RTS = 0004 TIOCM_ST = 0010 TIOCM_SR = 0020 TIOCM_CTS = 0040 TIOCM_CAR = 0100 TIOCM_CD = TIOCM_CAR TIOCM_RNG = 0200 TIOCM_RI = TIOCM_RNG TIOCM_DSR = 0400 TIOCREMOTE = (tIOC|30) TIOCSIGNAL = (tIOC|31) ISPTM = ((ord('P')<<8)|1) UNLKPT = ((ord('P')<<8)|2) SVR4SOPEN = ((ord('P')<<8)|100) LDIOC = (ord('D')<<8) LDOPEN = (LDIOC|0) LDCLOSE = (LDIOC|1) LDCHG = (LDIOC|2) LDGETT = (LDIOC|8) LDSETT = (LDIOC|9) LDSMAP = (LDIOC|10) LDGMAP = (LDIOC|11) LDNMAP = (LDIOC|12) DIOC = (ord('d')<<8) DIOCGETP = (DIOC|8) DIOCSETP = (DIOC|9) FIORDCHK = ((ord('f')<<8)|3)