1if(n) Tcl Built-In Commands if(n)
2
3
4
5______________________________________________________________________________
6
8 if - Execute scripts conditionally
9
11 if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else?
12 ?bodyN?
13______________________________________________________________________________
14
15
17 The if command evaluates expr1 as an expression (in the same way that
18 expr evaluates its argument). The value of the expression must be a
19 boolean (a numeric value, where 0 is false and anything is true, or a
20 string value such as true or yes for true and false or no for false);
21 if it is true then body1 is executed by passing it to the Tcl inter‐
22 preter. Otherwise expr2 is evaluated as an expression and if it is
23 true then body2 is executed, and so on. If none of the expressions
24 evaluates to true then bodyN is executed. The then and else arguments
25 are optional “noise words” to make the command easier to read. There
26 may be any number of elseif clauses, including zero. BodyN may also be
27 omitted as long as else is omitted too. The return value from the com‐
28 mand is the result of the body script that was executed, or an empty
29 string if none of the expressions was non-zero and there was no bodyN.
30
32 A simple conditional:
33
34 if {$vbl == 1} { puts "vbl is one" }
35
36 With an else-clause:
37
38 if {$vbl == 1} {
39 puts "vbl is one"
40 } else {
41 puts "vbl is not one"
42 }
43
44 With an elseif-clause too:
45
46 if {$vbl == 1} {
47 puts "vbl is one"
48 } elseif {$vbl == 2} {
49 puts "vbl is two"
50 } else {
51 puts "vbl is not one or two"
52 }
53
54 Remember, expressions can be multi-line, but in that case it can be a
55 good idea to use the optional then keyword for clarity:
56
57 if {
58 $vbl == 1
59 || $vbl == 2
60 || $vbl == 3
61 } then {
62 puts "vbl is one, two or three"
63 }
64
66 expr(n), for(n), foreach(n)
67
69 boolean, conditional, else, false, if, true
70
71
72
73Tcl if(n)