Switch in TCL

Like other programming languages, TCL also provides support to switch case. It is used to test multiple conditions. In TCL switch, we don’t need to write break statement like other languages. To achieve switch case functionality we can also use if else statements .

Let us see syntax of switch case :

switch option switch_expression

switch_expression can be a number, alphabet or string . If switch_expression match with any subsequent switch_expressions then body corresponding to that expression will be executed. default block will be executed if switch_expression doesn’t match with any subsequent expressions.

options can be :

-exact : Use exact matching (default)

-glob : When matching string to the patterns like *, ? (i.e. the same as implemented by the string match )

-regexp : When matching string to the patterns, use regular expression.

Let us see some examples :
1. switch case with numbers :

set number 5;
switch $number
1 {
puts "One"

2 {
puts "Two"

3 {
puts "Three"

default {
puts "Invalid Number."
 Output :
$tclsh main.tcl
Invalid Number.


2. Switch case with string :

set country_code "US";
switch $country_code {
US {
 puts "United States of America"
IN {
 puts "India"
UK {
 puts "United Kingdom"
default {
 puts "Invalid Country Code"
$tclsh main.tcl
United States of America


3. Switch using option:

set var_string computer;

switch -glob $var_string {
com*er {
 puts "Matched with com*er"
default {
 puts "Invalid !"

$tclsh main.tcl
Matched with com*er

Comments in TCL

We are  adding comments in source code to make it easier to understand or we can also use comments to add legal notices, like copyrights or to add some TODOs, Author name etc.

In TCL, we can add comments in three ways .

  1. Complete Line Comments:

For complete line comments, we need to use # sign in beginning. For example

# This line is commented...
puts "Hello World!"


2. Inline Comments:

Sometimes we need to write a comment just after finishing our code on the same line. In such cases, we can use inline comments.  Inline comments start with ;# in line. For example :

puts $tcl_version ;# This will print TCL Version


3. Block Comments:

Sometimes we need to add comments to the block of line or to the complete procedure. In such case, we can use block comments. We need to use if with condition 0 for block comments. For example :

puts "This line is not commented !"

if 0 {

This is an example of multi-line comments.

This complete block will be considered as comments.


puts "Even this is not comment"

Try to use these types of comments in TCL scripts !

If Else statement in TCL

Like other programming language TCL also support decision making using control structure like if -else , switch. As you might know that everything is command in TCL, including if-else and switch.

if statement validate expression and if expression is true then, block of statement written under if statement is executed. Here is syntax of if else:

if {boolean_expression} {
   # This block will be executed if boolean expression is true

One sample program based on above syntax:

set number 0

if { $number == 0 } {

puts "Number is zero"


Here if $number value is zero then expression will return true and as value of expression is true, statement written under if block will be executed.

Output :

C:\Tcl\bin>tclsh if_demo.tcl
Number is zero

Lets check how can we use if-else statement. Syntax of if-else statement:

if { boolean_expression } {
  # If the boolean expression is true this block will get executed
} else {
  # If the boolean expression is false this block will get executed

Simple program based on if-else condition to check whether number is positive or negative.

set number 2

if { $number > 0 } {

puts "Number is Positive"

} else {

puts "Number is Negative"


Output is:

C:\Tcl\bin>tclsh if_else_demo.tcl
Number is Positive

How to read input from console in TCL

You might know, we can use puts to write to console like this:
puts "Hello world !"

In this post we will see how to read keyboard input from console using TCL script. Some time we need to take input from user to proceed further, for example adding two numbers. Here is syntax to read :

gets channel_ID variable_Name

Here, channel_ID is id of channel . It specify from where we have to read. To read from console we need to use stdin (standard input) as channel ID.

variable_Name is name of variable where we have to store that text.

Let us check one sample example.

puts "what is your favourite colour ?"
gets stdin name_of_colour
puts "Colour is $name_of_colour"

Output :

C:\Tcl\bin>tclsh inputDemo.tcl
what is your favourite colour ?
Colour is red

Here is another example to add two numbers entered by user :

puts "Enter first number for addition:"
gets stdin first_number

puts "Enter second number for addition:"
gets stdin second_number

set answer [expr $first_number + $second_number]

puts "Addition of $first_number and $second_number is : $answer"


Output will be :

C:\Tcl\bin>tclsh addition.tcl

Enter first number for addition:
Enter second number for addition:
Addition of 33 and 11 is : 44

Data Types in TCL

TCL stores value as Object and every object having String representation and It may have secondary representation if needed.

Let me explain when this secondary represenation is used. For example you assign value to variable like this :

set a 5

TCl will create variable ‘a’ and assign 5 as string value . Now if you use variable ‘a’ where value of ‘a’ will be require as integer (for example arithmetic operations) then TCL will translate the string 5 into integer value. Value of 5 will remain integer until it is required as a string. Check following script and its comments for more detail.

set a 5             ; # a as string

set a [expr $a+4]   ; # a as integer

puts $a             ; # a as string 

See how can we use float value with TCL script .

set a 4.2

set a [expr $a+0.2]

puts $a ;# will print 4.4


List is basically string separated with whitespace . Curly braces or double quotes can be used to represent List.  

set listOfDay {monday tuesday wednesday}

puts $listOfDay ; # output => monday tuesday wednesday

set listOfOdd { 3 5 7 }

puts $listOfOdd ; # outout => 3 5 7


Associative Array

Associative arrays are like Key Value pair. Key can be string value .

set name(IN) "India"
set name(UK) "United Kingdom"

puts "Countries : $name(IN) $name(UK)"

# Output=> Countries : India United Kingdom