REPEAT

Establish a loop for testing; use with WHILE or UNTIL statement to test the loop at the middle or at the end. This construction eliminates the need for GO TO statements and the multiple tests that are required to form similar loops in COBOL.
Restriction: This topic applies only when the AppMaster Builder AddPack has been installed, and applies only to Windows platforms.

Syntax: for Format 1

REPEAT
    statementblock
UNTIL|WHILE condition
 [  statementblock    ]

Syntax: for Format 2

REPEAT VARYING|LINKING indexname|identifier1
... [FROM indexexpression]|[arithmeticexpression]
... [BY literal]|[identifier2]
    statementblock  
UNTIL|WHILE condition
 [  statementblock    ]

Syntax: for Format 3

REPEAT VARYING indexname|identifier1
... [FROM indexexpression][arithmeticexpression1]
... [BY literal]|[identifier2]
... [DOWN] TO|THRU arithmeticexpression2
 [  statementblock    ]

Syntax: for Format 4

REPEAT LINKING indexname|identifier1
... [FROM indexexpression][arithmeticexpression1]
... BY identifier2
... [DOWN] TO arithmeticexpression2
 [  statementblock    ]

Syntax: for Format 5

REPEAT VARYING|LINKING clause1
                      .
                      .
                      .
[... VARYING|LINKING clauseN]
 [  statementblock    ]
UNTIL|WHILE condition
 [  statementblock    ]

General Rules:

  1. The statement block subordinate to the REPEAT, and any statement block subordinate to the WHILE or UNTIL, forms the loop and executes under control of the conditions specified with WHILE or UNTIL.
  2. If the WHILE or UNTIL does not have a subordinate statement block, the condition is tested at the end of the loop. Control returns to the beginning of the REPEAT statement block until the WHILE condition is false or the UNTIL condition is true. The next statement executed is the first one with the same or less indentation than the REPEAT/WHILE or REPEAT/UNTIL.
  3. If the WHILE or UNTIL has a subordinate statement block, it establishes a loop which contains a test in the middle. When the WHILE condition is FALSE or the UNTIL condition is true, control passes out of the loop to the next statement with the same or less indentation than the REPEAT, without executing its statement block.
  4. In the following format, AMB tests the condition after the REPEAT statementblock1 executes.
    REPEAT
    
    statementblock
    UNTIL condition
    
    statementblock3
    
  5. Be careful using this format for reading records--it can read the last record twice.
  6. In the following format, AMB tests the condition after the REPEAT statementblock1 executes, but before the UNTIL statementblock2 executes. When the UNTIL condition is true, the UNTIL statemenblock2 does not execute.
    REPEAT
    statementblock
    UNTIL condition
    
    statementblock statementblock3
  7. Coding an extraneous WHILE or UNTIL causes an endless loop, if the looping condition is satisfied by the REPEAT VARYING/LINKING preceding it.
  8. DOWN is generally used if literal or identifier2 is negative at execution, and used if positive. The loop executes until:
    UNTIL indexname identifier1 < 
    arithmeticexpression2
    
  9. With the TO option, the loop executes to, but not including, the stop-point. With the THRU option, the loop executes through and including the stop-point.
  10. If FROM is not coded, the default is the value of indexname or identifier1 at the time of execution.
  11. If BY is not coded, the default is BY 1 (or -1, if DOWN TO/THRU is coded).
  12. AMB initializes the index or identifier is immediately before the REPEAT loop begins and increments it each time the loop repeats, immediately before the statement block repeats.
  13. For the identifier to be treated as an index, the indexed data element structure must be present in the program during AMB precompilation, otherwise subscript processing is assumed.
  14. To copy data containing an indexed structure, use the % INCLUDE statement.

Parameters:

arithmetic-expression

A legal arithmetic relation-condition

indexexpression

Format can be

literalidentifier +|- literalindexname

identifier2

Names a table entry, such as a data name with an OCCURS clause

Example:

Read header records from a file, move the relevant data to a table, and then print the table.
002010  NTRY
002020         LINE-SUB = 1
002030         PRINT-TABLE = SPACES
002040         OPEN INPUT INPUT-FILE
002050         ... OUTPUT PRINT-FILE
002060
002070         /* BEGIN FIRST LOOP
002080         REPEAT
002090             READ INPUT-FILE
002100         UNTIL AT END ON INPUT-FILE
002110             IF REC-TYPE = 'HDR'
002120                 WORK-FIELD = INPUT-DATA
002130                 IF WORK-FIELD NOT = SPACES
002140                     COLUMN-SUB = 0
002150                     /* BEGIN SECOND LOOP
002160                     REPEAT
002170                         COLUMN-SUB = COLUMN-SUB + 1
002180                         PRINT-COL (LINE-SUB, COLUMN-SUB) =
002190                         ... WORK-FIELD-CHAR (COLUMN-SUB)
002200                     UNTIL COLUMN-SUB = COLUMN-SUB-LIMIT
002210                     ... OR WORK-FIELD-CHAR (COLUMN-SUB) = 
002211                     ... '/'
002220                     /* END OF SECOND LOOP
002230                     LINE-SUB = LINE-SUB + 1
002240         /* END OF FIRST LOOP
002250
002260         PERFORM WRITE-PRINT-TABLE
002270         CLOSE INPUT-FILE PRINT-FILE
002280
002290  PARA   WRITE-PRINT-TABLE
002300             LINE-SUB = 1
002310             WHILE PRINT-LINE (LINE-SUB) NOT = SPACES
002320             ... AND LINE-SUB NOT > LINE-SUB-LIMIT
002330                 WRITE PRINT-REC FROM PRINT-LINE (LINE-SUB)
002340                 LINE-SUB = LINE-SUB + 1
Perform the same function as the second loop in the preceding example, but use VARYING to set and increment COLUMN-SUB.
    IF WORK-FIELD NOT = SPACES
        REPEAT VARYING COLUMN-SUB FROM 1 BY 1
            PRINT-COL (LINE-SUB, COLUMN-SUB) =
            ... WORK-FIELD-CHAR (COLUMN-SUB)
        UNTIL COLUMN-SUB = COLUMN-SUB-LIMIT
        ... OR WORK-FIELD-CHAR (COLUMN-SUB) = '/'
        LINE-SUB = LINE-SUB + 1
Use REPEAT ... VARYING to move data items in diagonal sequence (upper right to lower left) from a two-dimensional table to a one-dimensional table. Terminate the loop when after DOWN THRU.
    REPEAT VARYING ROW-SUB FROM 1 BY 1
    ... VARYING COLUMN-SUB FROM 5 DOWN THRU 1
        X-FIELD (ROW-SUB) =
        ... TABLE-ELEMENT (ROW-SUB, COLUMN-SUB)
Use II as the pointer, MY-CHAIN for the initial setting, BLOCK-LINK for the linking element in the table, and ZERO to establish when to stop.
             .
 01     BLOCK-STRUCTURE.
        02  BLOCK    OCCURS 250 TIMES.
            03 BLOCK-LINK  PIC S9(9) COMP SYNC.
            03 BLOCK-DATA  PIC X(20).
             .
 NTRY
        REPEAT LINKING II FROM MY-CHAIN
       ... BY BLOCK-LINK TO ZERO
           PERFORM PASS-DATA( BLOCK-DATA (II))
             .
 PARA  PASS-DATA(+PASS-DATA-BLOCK)
             .
Generated COBOL code:
    MOVE MY-CHAIN TO II.
    GO TO G--002.
G--001.
    MOVE BLOCK-LINK (II) TO II.
G--002.
    IF II NOT = ZERO
         MOVE BLOCK-DATA (II) TO PASS-DATA-BLOCK
         PERFORM PASS-DATA THRU PASS-DATA--XIT
         GO TO G--001.

AMB uses SET in the generated code if the subscript is an index; otherwise, AMB uses MOVE as shown above.