next up previous index
Next: Procedures Up: Basic Tcl Previous: Control Structures

Loops

for   init test next body
The usual for loop has no syntax sugar in Tcl:
% for {set i 1} {$i < 10} {incr i} {
    puts -nonewline "$i "
}    
1 2 3 4 5 6 7 8 9 %

foreach   varName list body
Iterate over all elements of a list:
% foreach i [info commands] {
    puts -nonewline [string index $i 0] 
} 
toepglpetuellgclpblarlecisaiacjlsgsacfcafafrsstswfcufrrueurhepills%

while   test body
While test is true, execute body:
% set a 10
10
% while {$a > 0} {
    lappend b $a
    incr a -1 
}
% set b
10 9 8 7 6 5 4 3 2 1

The commands break   and continue   have their usual meaning inside loops: break   leaves the nearest loop and continue   jumps to its beginning.



Micha Meier
Tue Jul 2 09:49:39 MET DST 1996