Control structures

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.  

if/else statements

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

for loop statement

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

}

while loop statement

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 } {

}

catch statement

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