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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set i 100 | |
if { $i < 20 } { | |
puts "I have less than 20 rupees" | |
} elseif { $i >= 20} { | |
puts "I have greater than 20 rupees in my pocket" | |
} else { | |
puts "I will never get into this condition" | |
} |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set i 100 | |
while { $i <= 100 } { | |
puts "Value of i is $i" | |
incr i -1 | |
} |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for { set i 10 } { $i < 10 } { incr i } { | |
puts "I love my country" | |
} |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set pins "inst1/a | |
inst2/b | |
inst3/a | |
inst4/a | |
mpu1/a | |
mpu2/b | |
" | |
set mpu_viopins "" | |
foreach pin $pins { | |
puts "pin name is $pin" | |
if { [regexp mpu $pin] } { | |
lappend mpu_viopins $pin | |
} | |
} | |
puts "Number of violating mpu pins : [llength $mpu_viopins]" |
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 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "Enter any number: " | |
flush stdout | |
set a [gets stdin] | |
switch $a { | |
5 { set a [expr $a*5] } | |
10 { set a [expr $a*6] } | |
default { set a 0 } | |
} | |
puts "Value of a is $a" |
No comments:
Post a Comment