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 certaintcl constructs. We will discuss about them in this section. Following are the different constructs which are available in TCL:
if - else & if - elseif - else structurewhile structureforeach structurefor structureswitch
- else & if - elseif - else structureif
If the condition is evaluated true, command1 and command2 will be executed.
Syntax :
command1
command2
}
You can use if-else structure as well. i.e. if t he condition is true do this and if it is false do that .
Syntax :
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 { c ond ition2{ }
com mand_n
}{ else
com mand_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:
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.
statement1;
statement2;
.
.
}
Example 4:
Switch
• "switch " is similar to if -
elseif - else structure, but it switches to and executes the first encountered and first satisfied pattern condition. It provides the compact and readable coding structure.
• Syntax:
The
variable value is tested with various patterns. The body of whichever pattern
matches is executed.
Example 5 :
No comments:
Post a Comment