Purpose: To discuss the various looping constructs available in Tcl
One can do looping via
for start test next command
for {set counter 1} {$counter <= 5} {incr counter} {
puts $counter
}foreach varList list ?varList list ...? command
foreach counter {1 2 3 4 5} {
puts $counter
}
# varList example
foreach {roman arabic} {
I 1
II 2
III 3
IV 4
V 5
VI 6
VII 7
VIII 8
IX 9
X 10
} {
puts "The roman numeral version of $arabic is $roman."
}
# multilist example
foreach x {1 2 3 4 5} y {6 7 8 9 10} {
puts "${x} x ${y} = [expr {$x*$y}]"
}while test command
set counter 1
while {$counter <= 5} {
puts $counter
incr counter
}or, if you have lots of breaking conditions, for example:
set counter 1
set start [ clock seconds ]
while { 1 } {
puts $counter
if { [ incr counter ] >= 50000 } {
break
}
if { [ expr {[ clock seconds ] - $start} ] >= 5 } {
break
}
}