Control Flow
- The structures are similar to C and UNIX csh shell
- They are implemented as commands, as any other thing in Tcl
- Remember: Tcl needs a space to tell one argument from another
- Braces are NOT separators (as in C and Perl)
- for init test reinit body
-
- Similar to the C and Perl implementation
- All the arguments are normal Tcl scripts
- The for command returns an empty string as the result
- foreach varName list body
-
- Basic form
- Iterates over all the elements of the list
- While executing the body, the current element is stored in the
variable
varName
- foreach varList1 valList1 [varList2 valList2 ...] body
- See TCL book for a full explanation.
- while test body
-
- Evaluate the test script as an expression
- Executes the body script if it's nonzero and then ...
- ... re-evaluate the test and loop
- Returns an empty string
- if test1 body1 [elseif test2 body2 ... else bodyn]
-
- Evaluate the test1 script as an expression
- If it's nonzero executes the body1 script and returns its value, otherwise ...
- ... evaluates the test2 script
- ... and so on...
- switch [options] string { pattern body [pattern body...] }
-
- Matches string against each pattern in order, until a match is found
- Executes the corresponding body and returns its value
- Doesn't keep on matching, as in C/C++
- If the last pattern is ``default'', it matches any value
- If a body is
-
then Tcl uses the body of the next pattern - Options are -exact, -glob, -regexp and --
- Use -- to tell that the next argument is the string, even if it begins with a dash (-)