for Statement

The for statement provides a mechanism to loop through or to repeat a series of program statements. In Privilege Management for Unix and Linux 2.8 and earlier, the for statement always terminates with an end statement. This is no longer necessary in Privilege Management for Unix and Linux 3.0+.

for ControlValue = StartValue to StopValue [step Increment]
{executable program statements}

The for statement works in the following manner:

  1. The first time through the for statement, ControlValue is set to StartValue.
  2. ControlValue is immediately compared to StopValue.
  3. After an execution of the for statement has been completed and all associated program statements have been executed, StartValue is incremented by the step value.
  4. If a step value is not specified, a default step value of 1 is used. ControlValue is again compared to StopValue and the result of this comparison determines if the for statement executes again.

The comparison of ControlValue to StopValue works as follows:

  1. When the Increment value is positive, the for statement is executed as long as ControlValue <= StopValue evaluates to true.
  2. When the Increment value is negative, the for statement is executed as long as ControlValue >= StopValue evaluates to true.
  3. When the Increment value is 0, the for statement executes forever. An accept or reject is required to break out of the loop.
  4. If an Increment is not specified, 1 is used as the increment value.

The for statement loop condition is tested at the top of the loop, and there is no guarantee the for loop will execute.

In the for statement
for LoopCounter = 0 to 10 step 1
{counter = counter + 1;
counter2 = counter2 + 2;
}

The statement continues to loop as long as LoopCounter is less than or equal to 10.

for LoopCounter = 0 to -5 step -1
{counter = counter + 1;
counter2 = counter2 + 2;
}

The for statement continues to loop as long as LoopCounter is greater than or equal to -5.