For loop in TCL

For loop in TCL can be achieved using the command for. It is used to execute code for multiple numbers of time.

Let’s see the syntax of for command:

for { start } { test } { next }

{

body

}

Here, start portion is used to initialise the variable. test is an expression, which will return either true or false . next is the portion to increment a variable, which has been written in the start portion.

Let’s see some examples:

  1. Increment by 1

# Example for loop

for {set i 0} {$i < 3} {incr i} {

puts "Value of i is: $i"

}

Output is :


Value of i is: 0
Value of i is: 1
Value of i is: 2

 

2. Increment by 2


# Example increment by 2

for {set i 0} {$i < 5} {incr i 2} {

puts "Value of i is: $i"

}

Output :


Value of i is: 0
Value of i is: 2
Value of i is: 4

 

3. Loop through in reverse direction:


# Loop through in reverse direction

for {set i 5} {$i > 0} {incr i -1} {

puts "Value of i is: $i"

}

Output:


Value of i is: 5
Value of i is: 4
Value of i is: 3
Value of i is: 2
Value of i is: 1

 

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_1 
{
body_1
}
switch_expression_2
{
body_2
}
switch_expression_3
{
body_3
}
default
{
default_body
}

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 :


#!/usr/bin/tclsh
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 :

#!/usr/bin/tclsh
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"
}
}
 Output:
$tclsh main.tcl
United States of America

 

3. Switch using option:

#!/usr/bin/tclsh
set var_string computer;

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

Output:
$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 ?
red
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:
33
Enter second number for addition:
11
Addition of 33 and 11 is : 44