Programming Guide



for

Executes one or more JPL statements the specified number of times

Synopsis

for counter = initValue while logicalExpr [ step stepValue] .[statementBlock]

Arguments

counter
A variable whose value may be tested as a condition for continuing or ending for execution

initValue
The initial value of counter

logicalExpr
Specifies the condition for continuing for execution. Execution remains inside the for loop until logicalExpr evaluates to false. You can specify multiple conditions with the logical operators AND (&&) and OR (||).

step stepValue
Optionally specifies the value by which counter is incremented or decremented, where stepValue is a positive or negative integer constant or variable. The default step value is 1. If stepValue is a variable, JPL evaluates it only once, before the first evaluation of logicalExpr. Subsequent changes in the value of the stepValue variable during loop execution have no effect on step processing.

statementBlock
One or more JPL statements to execute as long as logicalExpr evaluates to true. If statementBlock has multiple statements, enclose them with open and close blocking characters {0} on the lines before and after. If there is no statement to execute, enter a null statement {}.

Description

The for command starts a loop whose iterations increment a counter variable. Each for statement contains up to three clauses–initialization of the counter variable, a logical expression whose evaluation determines whether to reenter the loop, and optionally, the number by which to increment the counter variable. Panther executes a for statement as follows:

  1. Initializes counter to the value of initValue.
  2. Evaluates stepValue.
  3. Evaluates logicalExpr:

When the value of logicalExpr is false, JPL stops loop execution. In the simplest case, it compares counter to a value that specifies the number of times that JPL executes the loop. You can use other values to decide when loop execution ends. For example, you can use counter to evaluate array occurrences and use the value of an occurrence, like a null string, to the end the loop.

When you construct a logical expression, take into account that JPL, unlike C, always fully evaluates a boolean expression. For example, the following for statement traverses a screen's fields by field number (ct) until the last field or the first modified field is reached:

vars ct
vars n_flds = @screen("@current")->numflds

for ct = 1 while ct <= n_flds && @field_num(ct)->mdt == PV_NO

If all fields are unmodified, ct increments to one greater than n_flds on the last pass through the for loop, so the first condition evaluates to false; however, JPL also evaluates the second condition @field_num(ct), which is invalid. Consequently, JPL issues an error message and stops execution of the remaining code.

Caution

Keep the entire for command on the same physical line. Using continuation characters (\) to split a for command across several lines can yield unpredictable results.

Example

// Change each element of an array to its absolute value.
vars i
for i = 1 while i <= 10 step 1
{
if amounts[i] == ""
amounts[i] = "0"
else if amounts[i] < 0
amounts[i] = -amounts[i]
}

See Also

next,  break,  while