| [25] | 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 |
|---|
| 15 | if \- 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 |
|---|
| 22 | The \fIif\fR command evaluates \fIexpr1\fR as an expression (in the |
|---|
| 23 | same way that \fBexpr\fR evaluates its argument). The value of the |
|---|
| 24 | expression must be a boolean |
|---|
| 25 | (a numeric value, where 0 is false and |
|---|
| 26 | anything is true, or a string value such as \fBtrue\fR or \fByes\fR |
|---|
| 27 | for true and \fBfalse\fR or \fBno\fR for false); |
|---|
| 28 | if it is true then \fIbody1\fR is executed by passing it to the |
|---|
| 29 | Tcl interpreter. |
|---|
| 30 | Otherwise \fIexpr2\fR is evaluated as an expression and if it is true |
|---|
| 31 | then \fBbody2\fR is executed, and so on. |
|---|
| 32 | If none of the expressions evaluates to true then \fIbodyN\fR is |
|---|
| 33 | executed. |
|---|
| 34 | The \fBthen\fR and \fBelse\fR arguments are optional |
|---|
| 35 | .QW "noise words" |
|---|
| 36 | to make the command easier to read. |
|---|
| 37 | There 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. |
|---|
| 39 | The return value from the command is the result of the body script |
|---|
| 40 | that was executed, or an empty string |
|---|
| 41 | if none of the expressions was non-zero and there was no \fIbodyN\fR. |
|---|
| 42 | .SH EXAMPLES |
|---|
| 43 | A simple conditional: |
|---|
| 44 | .CS |
|---|
| 45 | \fBif\fR {$vbl == 1} { puts "vbl is one" } |
|---|
| 46 | .CE |
|---|
| 47 | .PP |
|---|
| 48 | With 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 |
|---|
| 57 | With 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 |
|---|
| 68 | Remember, expressions can be multi-line, but in that case it can be a |
|---|
| 69 | good 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" |
|---|
| 79 | expr(n), for(n), foreach(n) |
|---|
| 80 | |
|---|
| 81 | .SH KEYWORDS |
|---|
| 82 | boolean, conditional, else, false, if, true |
|---|