Previous Topic Next topic Print topic


Example - Looping

This program calls two user rules that create a data structure with loops. A loop that incorporates built-in functions returns a value to text.
Restriction: This topic applies only when the AppMaster Builder AddPack has been installed, and applies only to Windows platforms.

User rules:

%* HERE ARE TWO RULES: $ITEM-MAKER AND $TABLE-MAKER        601.
                                                           602.
 % DEFINE $ITEM-MAKER( &MM, &DATANAME, &TAIL)              603.
     % &II = 1                                             604.
     % WHILE &II <= &MM                                    605.
         02  &DATANAME+-&II      &40+PIC &TAIL.            606.
         % &II = &II + 1                                   607.
 % END                                                     608.
                                                           609.
 % DEFINE $TABLE-MAKER( &MM, &NN, &TNAME, &INAME, &TAIL)   610.
     % &II = 1                                             611.
     % WHILE &II <= &MM                                    612.
         02  &TNAME+-&II.                                  613.
         % &JJ = 1                                         614.
         % UNTIL &JJ > &NN                                 615.
             % BEGIN                                       616.
               03  &INAME+-&II+-&JJ &40+PIC &TAIL.         617.
             % &JJ = &JJ + 1                               618.
         % &II = &II + 1                                   619.
 % END                                                     620.
                                                           621.

Lines 603-608:

The rule $ITEM-MAKER has three formal arguments; the rule call on line 634 supplies the values. % WHILE starts a loop that executes repeatedly while the value of &II is <= to the value of &MM. The loop generates the 02 TEST-ITEM data elements (see line 634 under Program output).

Note the two uses of the plus symbol in line 606. Used in &DATANAME+-&II, the plus concatenates the two variables, resulting in TEST-ITEM-1 for the first data item. On the next line, &II increments by one during each loop, resulting in TEST-ITEM-2 for the second data item, etc. The second use for the plus symbol places the value of PIC &TAIL in column 40 of the output for each TEST-ITEM.

Lines 610-620:

The rule $TABLE-MAKER has five formal arguments whose values are supplied by the rule call in the program on line 635.

% WHILE starts an outer loop that repeatedly executes lines 613-619 while the &II <= &MM. The outer loop generates the 02 data elements ENTRY-1, ENTRY-2, and ENTRY-3.

& UNTIL starts a nested, inner loop. Its subordinate statement block (lines 616-618) executes repeatedly until &JJ > &NN. The inner loop generates the 03 data elements for the outer loop 02s.

% BEGIN places the 03 data items in the output shifted left four spaces in the inner loop, so that they are indented two spaces from the 02 data items. Without % BEGIN, they would have the same indentation as the 02s. The whole outer and inner loop structure shifts as a unit, thus all lines retain their relative positioning.

Line 618 increments the inner loop variable &JJ. Line 619 increments the outer loop variable &II; to be part of the outer loop, it must be indented from the outer loop but not from the inner loop. The loops generate the multiple lines; output from both loops appear in lines 634-635.

Program input:

 % SET BLANK                                               622.
 IDENTIFICATION DIVISION.                                  623.
 PROGRAM-ID.                     EXAMPLE6.                 624.
*SPECIAL CONSIDERATIONS.                                   625.
*    DEMONSTRATE LOOPING.                                  626.
                                                           627.
       ---------------------------------------             628.
       ---------------------------------------             629.
                                                           630.
 WORKING-STORAGE SECTION.                                  631.
                                                           632.
 01  TEST-RECORD.                                          633.
   $ITEM-MAKER( 12,'TEST-ITEM','S9(9) COMP SYNC VALUE -1') 634.
   $TABLE-MAKER( 3, 4,'ENTRY','ITEM' 'X(4) VALUE SPACES')  635.
 % &STR = 'MISSISSIPPI'                                    637.
 % &SUB = 'IS'                                             638.
 % &II = 0                                                 639.
 % REPEAT                                                  640.
     % &JJ = &INDEX( &STR, &SUB)                           641.
 % UNTIL &JJ = 0                                           642.
     % &II = &II + 1                                       643.
     % &JJ = &JJ + &LENGTH( &SUB)                          644.
     % &STR = &SUBSTR( &STR, &JJ)                          645.
 THE NUMBER OF 'IS' IN 'MISSISSIPPI' IS &II.               646.
                                                           647.

Line 622:

% SET BLANK makes all blank lines that follow appear in the output.

Lines 637-646:

The % REPEAT and % UNTIL structures use the built-in functions &INDEX, &LENGTH, and &SUBSTR to find the number of occurrences of the substring IS in the literal string MISSISSIPPI. Lines 641-645 execute repeatedly until &JJ = 0.

Program output:

062300 IDENTIFICATION DIVISION.                             623.
062400 PROGRAM-ID.                     EXAMPLE6.            624.
062500*SPECIAL CONSIDERATIONS.                              625.
062600*    DEMONSTRATE LOOPING.                             626.
062700                                                      627.
062800     -----------------------------------              628.
062900     -----------------------------------              629.
063000                                                      630.
063100 WORKING-STORAGE SECTION.                             631.
063200                                                      632.
063300 01  TEST-RECORD.                                     633.
063400   02  TEST-ITEM-1      PIC S9(9) COMP SYNC VALUE -1. 634.
063402   02  TEST-ITEM-2      PIC S9(9) COMP SYNC VALUE -1. 623.
063404   02  TEST-ITEM-3      PIC S9(9) COMP SYNC VALUE -1. 634.
063406   02  TEST-ITEM-4      PIC S9(9) COMP SYNC VALUE -1. 634.
063408   02  TEST-ITEM-5      PIC S9(9) COMP SYNC VALUE -1. 634.
063410   02  TEST-ITEM-6      PIC S9(9) COMP SYNC VALUE -1. 634.
063412   02  TEST-ITEM-7      PIC S9(9) COMP SYNC VALUE -1. 634.
063414   02  TEST-ITEM-8      PIC S9(9) COMP SYNC VALUE -1. 634.
063416   02  TEST-ITEM-9      PIC S9(9) COMP SYNC VALUE -1. 634.
063418   02  TEST-ITEM-10     PIC S9(9) COMP SYNC VALUE -1. 634.
063420   02  TEST-ITEM-11     PIC S9(9) COMP SYNC VALUE -1. 634.
063422   02  TEST-ITEM-12     PIC S9(9) COMP SYNC VALUE -1. 634.
063500   02  ENTRY-1.                                       635.
063502     03  ITEM-1-1       PIC X(4) VALUE SPACES.        635.
063504     03  ITEM-1-2       PIC X(4) VALUE SPACES.        635.
063506     03  ITEM-1-3         PIC X(4) VALUE SPACES.      635.
063508     03  ITEM-1-4         PIC X(4) VALUE SPACES.      635.
063510   02  ENTRY-2.                                       635.
063512     03  ITEM-2-1         PIC X(4) VALUE SPACES.      635.
063514     03  ITEM-2-2         PIC X(4) VALUE SPACES.      635.
063516     03  ITEM-2-3         PIC X(4) VALUE SPACES.      635.
063518     03  ITEM-2-4         PIC X(4) VALUE SPACES.      635.
063520   02  ENTRY-3.                                       635.
063522     03  ITEM-3-1         PIC X(4) VALUE SPACES.      635.
063524     03  ITEM-3-2         PIC X(4) VALUE SPACES.      635.
063526     03  ITEM-3-3         PIC X(4) VALUE SPACES.      635.
063528     03  ITEM-3-4         PIC X(4) VALUE SPACES.      635.
063600                                                      636.
064600 THE NUMBER OF 'IS' IN 'MISSISSIPPI' IS 2.            646.
064700                                                      647.
Previous Topic Next topic Print topic