Description
Executes scripts on a conditional basis.
Synopsis
if {expr1} [then] {body1} [elseif {expr2} [then] {body2}] [elseif ...] [else {bodyN}]
Options
Defines the conditional expression. For a numeric value, 0 is false and anything other than 0 is true. For a string value, true or yes is true and false or no is false. Do not put a comment into conditional expression.
Condition operators available for conditional expressions
! | NOT |
< , > , <= , >= , == , != | Comparison |
&& , || | Logical AND, logical OR |
The script command executed when the condition is true. Two or more commands can be written by being separated by line breaks or semicolons.
Caution
Keywords (if, then, elseif, and else) must be separated from "{" by blanks or tabs. If "then" is omitted, "}" must be separated from "{" by blanks or tabs.
When inserting a line break in other than body such as between "}" and "{", add backslash sign (\) to the last of the line to indicate continuation.
Incorrect example: A line break is inserted in other than body
if {$cnt == 1 } { puts "start" }
Correct example: No line break is inserted between "}" and "{"
if {$cnt == 1 } { puts "start" }
Or, a line break is inserted and a backslash (\) is added to indicate line continuation.
if {$cnt == 1 } \ { puts "start" }
Example
The following example determines whether the value of variable a is greater than 0 or not and displays the result.
if {$a > 0} { puts "a > 0" } else { puts "a <= 0" }
Execution Results/Output Format
Either of the following lines is output depending on the value of a.
a > 0
or,
a <= 0