Showing posts with label tcl. Show all posts
Showing posts with label tcl. Show all posts

Sunday, December 6, 2015

Control Structures

Control Structures : (Under construction)

In general execution of the program would be sequential, if the programmer needs to change the flow of execution, he can very well do using certain tcl constructs. We will discuss about them in this section. Following are the different constructs which are available in TCL:

  • if - else & if - elseif - else structure
  • while structure
  • foreach  structure
  • for structure
  • switch





  • if - else & if - elseif - else structure
     If the condition is evaluated true, command1 and command2 will be executed.
     Syntax :
       if { condition } {
       command1
       command2
       }


     You can use if-else structure as well. i.e. if the condition is true do this and if it is false do that.
    Syntax :
     if { condition } {
       command1
       command2
      } else {
        command3
        command4
     }

You can use if-elseif-else and even nested if within each group.

If condition1 is evaluated to true, command_m would be executed, else if condition2 is evaluated to true command_n. If neither the condition evaluates to true, command_p would be executed



   if { condition1 } {
  command_m  
 } elseif { condition2 } { 
  command_n    
} else {  
command_p  
  }
Example 1:



While loop :

  • While the condition evaluates to true iterate over the loop again and again till it evaluates to false. Once it evaluates to false come out of the loop.



while { condition } {
statement1;statement2;updation; }


Example 2:


For loop :
It is similar to the while loop except that you can have a flexibility to  initialize the loop iterator and also update the iterator in the same line.


for { initiliaze } { condition } { updation } {
command1;command2;..}

Example 3:


foreach control structure:
It loops over a set of variables in the list and executes operations on each variable. It is the most commonly used control structure in the TCL.


foreach var <list> {

execute operation on var;
statement1;
statement2;
.
.
}

Example 4:


TCL arrays

TCL array: (Under construction)

  • Array is a collection of variables.
  • Associative  -> abstract data type composed of a collection of key value pairs. 
  • Key value pairs are not ordered.
  • Each variable can hold any value or an array.

How to create an array in tcl?
  • We can create an array using set or "array set"
Array operations :
Different operations associated with array


array exists
array get
array names
array set
array size
array unset
array startsearch
array nextelement
array anymore
array donesearch
array statistics



Output: 
sat and Saturday are same
wed and Wednesday are same
fri and Friday are same
thu and Thursday are same
sun and Sunday are same
tue and Tuesday are same
mon and Monday are same

Note: Observe that key and value pairs are not ordered.

Example:2

Output:
4 is associated to  Thu
5 is associated to  Fri 
1 is associated to  Mon 
6 is associated to  Sat 
2 is associated to  Tue 
7 is associated to  Sun 
3 is associated to  Wed

All array operations are listed in this example
Example:3


Example:4