For

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 6.  Control Structure Commands


For

The for command is similar to the C for statement. It takes four arguments:

 for initial test final body 

The first argument is a command to initialize the loop. The second argument is a boolean expression that determines whether the loop body will execute. The third argument is a command to execute after the loop body:

Example 6-13 A for loop.
 for {set i 0} {$i < 10} {incr i 3} {    lappend aList $i } set aList => 0 3 6 9 

You could use for to iterate over a list, but you should really use foreach instead. Code like the following is slow and cluttered:

 for {set i 0} {$i < [llength $list]} {incr i} {     set value [lindex $list $i] } 

This is the same as:

 foreach value $list { } 

       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net