12.5.14    for

Description:   ‘‘For’’ loop, Iterate body of commands until test fails

Syntax:         for start test next body

         

Argument:     start value, logical test expression, and variable to be incremented, command body

Returns:         empty string

See Also:       SCRLOOP, while, foreach, break, continue

 

Examples:      #example 1 - will ramp the value of tag2 from 0 to 9

 

for {set x 0} {$x<10} {incr x} {

SETVAL "tag2 = $x"

}

For is a looping command that runs until the test is false. The body of the command will execute until the test becomes false. Commands in the script that follow the For Loop will not execute until the For Loop ends.

 Next is used to increment a variable used in the test. The start, next, and body arguments must be Tcl command strings, and test is an expression string. The for command repeatedly evaluates test as an expression; if the result is non-zero it executes the command body, then executes the next statement, then repeats the loop. The command terminates when test evaluates to 0 (false). If a continue command is invoked within body then any remaining commands in the current execution of body are skipped; processing continues by invoking the Tcl interpreter on next, then evaluating test, and so on. If a break command is invoked within body or next, then the for command will return immediately.   for returns an empty string.

Note: the test expression should almost always be enclosed in braces. If not, variable substitutions will be made before the for command starts executing, which means that variable changes made by the loop body will not be considered in the expression. This is likely to result in an infinite loop. If test is enclosed in braces, variable substitutions are delayed until the expression is evaluated (before each loop iteration), so changes in the variables will be visible