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