1if(n)                        Tcl Built-In Commands                       if(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       if - Execute scripts conditionally
9

SYNOPSIS

11       if  expr1  ?then?  body1  elseif  expr2  ?then? body2 elseif ... ?else?
12       ?bodyN?
13_________________________________________________________________
14
15

DESCRIPTION

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

EXAMPLES

32       A simple conditional:
33              if {$vbl == 1} { puts "vbl is one" }
34
35       With an else-clause:
36              if {$vbl == 1} {
37                 puts "vbl is one"
38              } else {
39                 puts "vbl is not one"
40              }
41
42       With an elseif-clause too:
43              if {$vbl == 1} {
44                 puts "vbl is one"
45              } elseif {$vbl == 2} {
46                 puts "vbl is two"
47              } else {
48                 puts "vbl is not one or two"
49              }
50
51       Remember, expressions can be multi-line, but in that case it can  be  a
52       good idea to use the optional then keyword for clarity:
53              if {
54                 $vbl == 1 || $vbl == 2 || $vbl == 3
55              } then {
56                 puts "vbl is one, two or three"
57              }
58
59

SEE ALSO

61       expr(n), for(n), foreach(n)
62
63

KEYWORDS

65       boolean, conditional, else, false, if, true
66
67
68
69Tcl                                                                      if(n)
Impressum