Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/if.n @ 33

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

added tcl to libs

File size: 2.4 KB
Line 
1'\"
2'\" Copyright (c) 1993 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 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: if.n,v 1.9 2007/12/13 15:22:32 dgp Exp $
9'\"
10.so man.macros
11.TH if n "" Tcl "Tcl Built-In Commands"
12.BS
13'\" Note:  do not modify the .SH NAME line immediately below!
14.SH NAME
15if \- Execute scripts conditionally
16.SH SYNOPSIS
17\fBif \fIexpr1 \fR?\fBthen\fR? \fIbody1 \fBelseif \fIexpr2 \fR?\fBthen\fR? \fIbody2\fR \fBelseif\fR ... ?\fBelse\fR? ?\fIbodyN\fR?
18.BE
19
20.SH DESCRIPTION
21.PP
22The \fIif\fR command evaluates \fIexpr1\fR as an expression (in the
23same way that \fBexpr\fR evaluates its argument).  The value of the
24expression must be a boolean
25(a numeric value, where 0 is false and
26anything is true, or a string value such as \fBtrue\fR or \fByes\fR
27for true and \fBfalse\fR or \fBno\fR for false);
28if it is true then \fIbody1\fR is executed by passing it to the
29Tcl interpreter.
30Otherwise \fIexpr2\fR is evaluated as an expression and if it is true
31then \fBbody2\fR is executed, and so on.
32If none of the expressions evaluates to true then \fIbodyN\fR is
33executed.
34The \fBthen\fR and \fBelse\fR arguments are optional
35.QW "noise words"
36to make the command easier to read.
37There may be any number of \fBelseif\fR clauses, including zero.
38\fIBodyN\fR may also be omitted as long as \fBelse\fR is omitted too.
39The return value from the command is the result of the body script
40that was executed, or an empty string
41if none of the expressions was non-zero and there was no \fIbodyN\fR.
42.SH EXAMPLES
43A simple conditional:
44.CS
45\fBif\fR {$vbl == 1} { puts "vbl is one" }
46.CE
47.PP
48With an \fBelse\fR-clause:
49.CS
50\fBif\fR {$vbl == 1} {
51   puts "vbl is one"
52} \fBelse\fR {
53   puts "vbl is not one"
54}
55.CE
56.PP
57With an \fBelseif\fR-clause too:
58.CS
59\fBif\fR {$vbl == 1} {
60   puts "vbl is one"
61} \fBelseif\fR {$vbl == 2} {
62   puts "vbl is two"
63} \fBelse\fR {
64   puts "vbl is not one or two"
65}
66.CE
67.PP
68Remember, expressions can be multi-line, but in that case it can be a
69good idea to use the optional \fBthen\fR keyword for clarity:
70.CS
71\fBif\fR {
72   $vbl == 1 || $vbl == 2 || $vbl == 3
73} \fBthen\fR {
74   puts "vbl is one, two or three"
75}
76.CE
77
78.SH "SEE ALSO"
79expr(n), for(n), foreach(n)
80
81.SH KEYWORDS
82boolean, conditional, else, false, if, true
Note: See TracBrowser for help on using the repository browser.