TCL – Control Flow
Dr. C. Prayline Rajabai
Assistant Professor Senior
School of Electronics Engineering
Vellore Institute of Technology
Overview
Tcl’s control flow commands are similar to the control flow statements in the C programming language,
csh and perl script.
Control flow statements in TCL include if, while, for, foreach, switch, and eval.
2 VIT - SENSE 09-01-2024
The if command
The if command evaluates an expression, tests its result, and conditionally executes a script based on
the result.
if command receives two arguments. First is an expression and the second is a Tcl script.
if command can also include one or more elseif clauses with additional tests and scripts, plus a final
else clause with a script to evaluate if no test succeeds.
Example
if {$x < 0} {
...
} elseif {$x == 1} {
...
} else {
...
}
3 VIT - SENSE 09-01-2024
Looping commands
Tcl provides three commands for looping: while, for, and foreach.
All these commands are similar to the perl script.
While Command :
The while command takes two arguments: an expression and a Tcl script.
It evaluates the expression and if the result is non-zero then it executes the Tcl script.
This process repeats over and over until the expression evaluates to zero, at which point the while
command terminates and returns an empty string.
4 VIT - SENSE 09-01-2024
Looping Commands
Example program to reverse the elements of a list:
set a [list x y z]
set b ""
set i [expr [llength $a] -1]
while {$i >= 0} {
lappend b [lindex $a $i]
incr i -1
5 VIT - SENSE 09-01-2024
Looping Commands
For Command
The for command is similar to while except that it provides more explicit loop control.
Example program to reverse the elements of a list using for command:
set a {w x y z}
set b ""
for {set i [expr [llength $a]-1]} {$i >= 0} {incr i -1} {
lappend b [lindex $a $i]
6 VIT - SENSE 09-01-2024
Looping Commands
The foreach command iterates over all of the elements of a list.
Foreach takes three arguments. The first is the name of a variable, the second is a list, and the third is a
Tcl script that forms the body of the loop.
Foreach will execute the body script once for each element of the list, in order.
Before executing the body in each iteration, foreach sets the variable to hold the next element of the list.
Example (list reversal)
set a {w x y z}
set b "";
foreach i $a {
set b [linsert $b 0 $i]
7 VIT - SENSE 09-01-2024
Loop control: break
The break command causes the innermost enclosing looping command to terminate immediately.
(Similar to ‘last’ in perl)
Example:
suppose the list reversal example above it is desired to stop as soon as an element equal to a particular
value is found in the source list then we can use the break command.
set b "";
foreach i $a {
if {$i == "ZZZ"} break
set b [linsert $b 0 $i]
8 VIT - SENSE 09-01-2024
Loop control: continue
The continue command causes only the current iteration of the innermost loop to be terminated.
(Similar to ‘next’ in perl)
The loop continues with its next iteration.
Example :
set b ""
foreach i $a {
if {$i == "ZZZ"} continue
set b [linsert $b 0 $i]
9 VIT - SENSE 09-01-2024
switch command
The switch command tests a value against a number of patterns and executes one of several
Tcl scripts depending on which pattern matched.
The same effect as switch can be achieved with an if command that has lots of elseif clauses,
but switch provides a more compact encoding.
Tcl’s switch command has two forms.
10 VIT - SENSE 09-01-2024
switch command
First form
switch $x {a {incr t1} b {incr t2} c {incr t3}}
The first argument to switch is the value to be tested.
The second argument is a list containing one or more pairs of elements.
The first argument in each pair is a pattern to compare against the value.
The second is a script to execute if the pattern matches.
The switch command steps through these pairs in order.
As soon as it finds a match it executes the corresponding script and returns the value of that script as its
value.
If no pattern matches then no script is executed and returns an empty string.
11 VIT - SENSE 09-01-2024
Switch Command
Second Form :
This form spreads the patterns and scripts into separate arguments.
switch $x a {incr t1} b {incr t2} c {incr t3}
First form is preferable because you can easily spread the patterns and scripts across multiple lines
like this:
switch $x {
a {incr t1}
b {incr t2}
c {incr t3}
12 VIT - SENSE 09-01-2024
Switch Command
If a script in a switch command is “-” then switch uses the script for the next pattern instead.
Example :
switch $x {
a-
b-
c {incr t1}
d {incr t2}
This script increments variable t1 if x is a, b, or c and it increments t2 if x is d.
13 VIT - SENSE 09-01-2024
Eval Command
Eval is a general-purpose building block for creating and executing Tcl scripts.
It accepts any number of arguments, concatenates them together with separator spaces, and then
executes the result as a Tcl script.
One use of eval is for generating commands, saving them in variables, and then later evaluating the
variables as Tcl scripts.
Example :
set cmd "set a 0"
...
eval $cmd
In the above example variable a is set to 0 when the eval command is invoked.
Perhaps the most important use for eval is to force another level of parsing.
14 VIT - SENSE 09-01-2024
Eval Command
The Tcl parser performs only one level of parsing and substitution when parsing a command.
The results of one substitution are not reparsed for other substitutions.
eval provides the mechanism to achieve this.
Suppose a variable vars contains a list of variables and you wish to unset each of these variables then the
following script will not work.
unset $vars
The solution is to use eval, as below
eval unset $vars
Eval generates a string consisting of “unset ” followed by the list of variable names and then passes the string
to Tcl for evaluation.
15 VIT - SENSE 09-01-2024
Source command
Source command reads a file and executes the contents of the file as a Tcl script.
It takes a single argument that contains the name of the file.
Example :
source init.tcl
The above command will execute the contents of the file init.tcl.
16 VIT - SENSE 09-01-2024
Exercise
Write a program that asks the user for the outside temperature in degrees Celsius. Display the
equivalent in degrees Celsius. The conversion formula is:
F=32+9C/5
where F is degrees Fahrenheit and C is degrees Celsius. Then, if the temperature is going to be below
40F tell the user to take a coat. If the temperature is above 80F tell them to avoid sunburn. If it’s in
between, tell them it will be a great day!
17 VIT - SENSE 09-01-2024
Program
puts "Enter the temperature in celsius"
gets stdin c;
set f [expr (($c*9/5)+ 32)]
puts “$c degree Celsius is = $f degree Fahrenheit”
if {$f<40} {
puts "Temperature is very low. Please take a coat"
} elseif {$f>80} {
puts "Temperature is very hot. Don't go out.. Avoid sunburns"
} else {
puts "Have a great day!"
18 VIT - SENSE 09-01-2024
Thank You…