next up previous
Next: Executing Tcl-source Up: An introduction to Previous: Procedures

Control flow

The most commonly used control flow constructs of Tcl:

break
Abort innermost containing loop command.
continue
Skip to the next iteration of innermost containing loop command.
exit [ returnCode ]
Terminate the process, returning returnCode (an integer which defaults to 0) to the system as the exit status.
for start test next body
Looping command where start, next, and body are Tcl command strings and test is an expression string to be passed to expr command.
Example:
for {set i 0} {i < 10} {incr i} {puts i}
(This example makes use of deferred substitution; everything between {} is passed unsubstituted to the for command.)
if expr1 then body1 [ elsif expr2 then body2 ... ] [ else body2 ]
If expression string exptr1 evaluates to true, Tcl command string body1 is evaluated. Otherwise if expr2 is true, body2 is evaluated, and so on. If none of the expressions evaluate to true then bodyN is executed.
Example:
if [expr i == 27] then {puts "i = 27"} else {puts "i != 27"}
return [string]
Return immediately from current procedure with string as return value.
while test body
Evaluates the Tcl command string body as long as the expression string test evaluates to true.
Example:
set i 0
while {i < 10} {incr i; puts i}
(This is another example of using deferred substitution.)



SE Praktikum
Tue Aug 13 13:33:30 MET DST 1996