PreviousNext

The for Loop

The for loop also behaves just like its C counterpart. Although for is more complex than its sibling while, for keeps all of the loop control information together, making it easier to see what's going on. The for command syntax is

for initial_expression test reinit script_body

To use for, set an initial expression and then test for that condition before executing the script body. After executing the script body, the for command reinitializes the initial expression and again, tests for the new value, repeating the loop until the test becomes false.

The following example shows a for loop that does an operation a specified number of times and stops. In this example, we create fifty guest principal names in the registry.

dcecp> for {set i 0} {$i < 50} {incr i} {
> principal create guest$i
> }
dcecp>