BEG LOOP


The BEG LOOP statement initiates the execution of a loop to repeat the same processing logic for a specified number of times. A matching END LOOP statement is always used in conjunction with a BEG LOOP to end the loop.

    ••••• BEG LOOP •• = ••• TO •••  STEP •••
    (1)            (2)  (3)    (4)       (5)

(1) T/F execution conditions

(2) Loop index (A to Z, AI to ZI)

(3) "From" value (constant/index)

(4) "To" value (constant/index)

(5) "Step" value (constant/index)

Using the Statement

BEG LOOP incorporates an index counter that is initialized to the starting or “from” value. Looping continues until the index counter is greater than the ending or “to” value (unless the increment has a minus sign, in which case the “from” value is higher than the “to” value and looping stops when the counter is less than the “to” value). The increment or “step” value is added to the index counter following each execution of the loop.

The index counter for the loop can be any of the predefined fields A to Z (variable decimal) or AI to ZI (integer). The “from,” “to,” and “step” values can be any of the predefined fields A to Z or AI to ZI, or they can be specified as a constant integer value, either positive or negative.

Upon completion of the loop, processing resumes with the statement following the matching END LOOP statement.

If the step value is zero, the loop will continue to execute until a GOTO or END statement is encountered. You can also “break out” of the loop by making the END LOOP statement conditional by using T/F indicators. If a condition is met that prohibits the END LOOP statement from being executed, the END LOOP statement is bypassed and the loop immediately ends.

If the A to Z predefined fields are used for the “from,” “to,” or “step” values, you can increment the step in fractional amounts.

Restrictions

Every loop must eventually be ended by a matching END LOOP using the same index counter.

Note that, although you are allowed to mix data types within a BEG LOOP statement, performance may degrade slightly.

Compilation Errors

The system generates a compile error if a matching END LOOP is not found after a BEG LOOP statement, or if an unmatched END LOOP is detected.

Statement Ignored

If the “step” value is positive and the “from” value is greater than the “to” value or, if the “step” value is negative and the “from” value is less than the “to” value, the entire loop is not executed.

Related Statements

END, END LOOP, GOTO

Example

The following example is a simple loop that increments the current monthly budgets by 5 percent for all 12 months:

          BEG LOOP AI = 001 TO 012  STEP 001
          COMPUTE  TGL BUDGET AMOUNT          AI  *      1.05
          END LOOP AI

After the twelfth repetition (where AI=12), control is passed to the next statement following the END LOOP statement.

The next example presents a nested loop that repeats its processing logic within each occurrence of the main loop. This loop also incorporates a conditional check to see if the value of field C equals that of field D, at which point the internal loop terminates and returns control to the main loop.

          BEG LOOP A  = 001 TO 010  STEP 001
          *
          *        (Perform processing if needed)
          *

          BEG LOOP B  = 001 TO 005  STEP 001
          *
          *        (Perform processing)
          *
          IF       --- C                          EQ --- D
    T     SET      --- B                          =      5
          END LOOP B
          *
          END LOOP A

There can be more BEG LOOP statements in an ILF routine than END LOOP statements. For example:

    T     BEG LOOP A  = 001 TO 010  STEP 001
    F     BEG LOOP A  = 001 TO 005  STEP 001
          *
          *        (Perform processing)
          *
          END LOOP A

In the preceding example, only one of the BEG LOOP statements can execute.

Note also that mixing integer and variable decimal data types is allowed. This means that the following types of statements can be defined:

          BEG LOOP AI = B   TO CI   STEP D
    or
          BEG LOOP A  = 001 TO XI   STEP ZI

In both of these examples, the number of repetitions and the step-values are variables, with values set within the context of the ILF routines.