Constructs as commands
All constructs in C are like commands in TCL.
« Previous Next »
For instance: if, while, for, foreach
if construct:
Syntax: 'if test body'
Note: If test and body part has many words as to require multiple words, enclose them in braces { }. Enter key (newline or space characters) are escaped inside braces.
Example:
% if 5 {
puts 4
}
% if { $x > $y} {
puts $y
} else {
puts $x
}
Here, 'elseif' and 'else' are like arguments for 'if' command.
while construct:
Syntax: 'while test body'
Example:
% while {$x < $y} {
set x [expr $x + 1]
puts $x
}
for construct:
Syntax: 'for init test update body'
Example:
% for {i=0; j=5} {$i < $j} {incr i 2} {
puts $i
}
foreach construct:
Syntax: 'foreach varname list body'
List can be scalar or list. Also, list can be literal or can contain variables or another list.
Example:
% foreach names {rohit shikhar virat ajinkya} {
puts $names
}
%set L1 {rohit shikhar virat ajinkya}
% foreach names $L1 {
puts $names
}
switch case construct:
Syntax: 'switch option varname'
Valid options are: exact, glob, regexp, nocase, matchvar VarName...
No fall through in cases
Default is optional
Example:
%switch name {
virat {puts "Best batsman in cricket"}
smith{puts "Best batsman in test cricket"}
rohit {puts "Best batsman in T20 cricket"}
root -
default {puts "No option selected"}
}
Comments
Post a Comment