12.5.19    if

Description:   Execute scripts conditionally.

Syntax:         if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else? ?bodyN?

 

if expr1 body1

if expr1 then body1

                   if expr1 then body1 else bodyN

if expr1 then body1 elseif expr 2 then body2

if expr1 then body1 elseif expr 2 then body2 else bodyN

 

Argument:     logical expression  and command  

Returns:        The return value of the body of the script that was executed.

See Also:       else, elseif

 

Examples:     

catch {

 

if {[GETVAL AMPLITUDE] < 0} then {

   SETVAL {AMPLITUDE=%LOOPPLUS 10}

} elseif {[GETVAL AMPLITUDE] < [GETVAL AMPLITUDE.OUTPHI]} then {

   SETVAL {AMPLITUDE=%LOOPPLUS @tag1}

} else {

   SETVAL {AMPLITUDE=@AMPLITUDE.OUTPLO}

}

}

# ramps value up to output high limit.  if value less than 0, increase by 10, else increase by value of tag1 until output high limit reached, then reset to output low limit.

 

The if command tests for True or False.  It will execute  a command if the test is true.  Optionally it will provide alternate tests and alternate commands to perform.

If evaluates expr1 as an expression the same way that expr would evaluate it. The value of the expression must be a Boolean (a numeric value, where 0 is false and anything is true, or a string value such as true or yes for true and false or no for false); if it is true then body1 is executed by passing it to the Tcl interpreter. Otherwise expr2 is evaluated as an expression and if it is true then body2 is executed, and so on. If none of the expressions evaluates to true then the else bodyN is executed. The then and else arguments are optional ‘‘noise words’’ to make the command easier to read.

There may be any number of elseif clauses, including zero. BodyN may also be omitted as long as else is omitted too. The return value from the command is the result of the body script that was executed, or an empty string if none of the expressions was non-zero and there was no bodyN.

 Tcl is very picky about spaces and line breaks. The if-then-else statements seem to work best when if and then are on same line, and the else is on same line as the then command body.

if {} then {

} elseif {} then {

} else {

}

Hint  If you use the script editor dialog box keywords,  and double click on if, then double click on elseif, it will position the if, then elseif with line breaks appropriately so they always work.

 

Then and else are optional. For example

if {$var == 1} {SETVAL tag1=100}

works just like if {$var == 1} then {SETVAL tag1=100}

AND

if {$var == 1} {SETVAL tag1=100} {SETVAL tag1=0}

works just like

 if {$var == 1} then {SETVAL tag1=100} else {SETVAL tag1=100}