21.1.10 Control Structures
(Ask a Question)Tcl control structures are commands that change the flow of execution through a script. These control structures include commands for conditional execution (if-then-elseif-else) and looping (while, for, catch).
An "if" statement only executes the body of the statement (enclosed between curly braces) if the Boolean condition is found to be true.
21.1.10.1 if/else Statements
(Ask a Question)if { “$name” == “paul” } then {
…
# body if name is paul
} elseif { $code == 0 } then {
…
# body if name is not paul and if value of variable code is zero
} else {
…
# body if above conditions is not true
}
21.1.10.2 for Loop Statement
(Ask a Question)A "for" statement will repeatedly execute the body of the code as long as the index is within a specified limit.
for { set i 0 } { $i < 5 } { incr i } {
…
# body here
}
21.1.10.3 while Loop Statement
(Ask a Question)A "while" statement will repeatedly execute the body of the code (enclosed between the curly braces) as long as the Boolean condition is found to be true.
while { $p > 0 } {
…
}
21.1.10.4 catch Statement
(Ask a Question)A "catch" statement suspends normal error handling on the enclosed Tcl command. If a variable name is also used, then the return value of the enclosed Tcl command is stored in the variable.
catch { open “$inputFile” r } myresult