DO loops make it much easier to write routines that are performed more than once. For example, to count the occurrences of a character in a character string, the DO and IF operations can be used to process an array. See Figure 7.7.
.....DName+++++++++++EUDS.......Length+TDc.Functions++++++++++++++++++++++++++++ 0001 D Text S 1A DIM(80) CTDATA PerRcd(80) .....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq 0002 C Do 80 X 3 0 0003 C If text(x) = 'Q' 0004 C ADD 1 Count 3 0 0005 C EndIf 0006 C endDo **CTDATA TEXT Quality Programmers write Quality code, not Quantity code.
Figure 7.7 illustrates a typical programming task: counting the occurrences of a letter contained in an array. The array TEXT defined on line 1 is an 80-element array. Each element is one character in length.
The DO loop started on line 2 is performed 80 times. The field X is used as the counter index of the DO loop and is used for the index of the array TEXT (line 3).
The array element x is compared to the letter Q on line 3. If the array element equals the letter Q, the value 1 is added to the field COUNT (line 4). When the DO loop completes its 80th pass, the field COUNT is set to the number of occurrences of the letter Q that are found in the array TEXT.
DO loops can be used in several areas. For example, if an older program that uses level-break processing is in need of maintenance, the DO operation can be used to improve performance.
As mentioned earlier, one compare instruction is generated for each conditioning and level-break indicator. Therefore, programs using level-break processing—which uses the same level-break indicator on several consecutive program lines—will perform poorly.
The DO operation can be used to condition level-break processing. By conditioning only the DO operation on the level-break indicator and the subsequent operations on indicator L0, the program's performance improves. See Figures 7.8 and 7.9.
.....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq CL1 DO CL0 MOVE FLDA FLDB CL0 ADD AMT TOTAL CL0 MOVE '**' FLAG2 CL0 MOVE '***' FLAG3 CL0 EXCEPT SubTotal CL0 Eval Tqty = 0 CL0 Eval Gqty = 0 CL0 ENDDO
.....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq CL1 MOVE FLDA FLDB CL1 ADD AMT TOTAL CL1 MOVE '**' FLAG2 CL1 MOVE '***' FLAG3 CL1 EXCEPT SubTotal CL1 Eval Tqty = 0 CL1 Eval Gqty = 0
Figure 7.8 is the preferred technique because only one indicator test is performed. L0 (an indicator that is, by definition, always on) is used in place of redundant L1 indicators. Therefore, the compiler avoids generating code that tests the status of the indicator on each line of code.
In Figure 7.9, one compare instruction is generated for each occurrence of indicator L1. Thus, seven indicators are present and so seven compare operations are generated.
The DO WHILE and DO UNTIL operations provide a high-level of flexibility for application development. DOW (DO WHILE) is used, while a condition is true, to perform a routine repetitively. DOU (DO UNTIL) is used, until a condition is true, to perform a routine repetitively. For example, the DOU operation can be used with the READ operation to read a file until a field contains a specific value. See Figure 7.10.
.....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq C Dou Dept = 'Q38' or %EOF = *ON C Read EMPLOYEE C endDo
In Figure 7.10, the DOU operation is used to find the first occurrence of the department 'Q38' in the file EMPLOYEE. The OR operation extends the DO UNTIL to include a test for an end-of-file condition.
The same function can be performed using the DOW operation. The condition test must be reversed, however, because the loop is performed while the condition is true. See Figure 7.11.
.....CSRn01Factor1+++++++OpCode(ex)Factor2+++++++Result++++++++Len++DcHiLoEq C DoW Dept 'Q38' AND NOT %EOF C Read EMPLOYEE C endDo
When using these techniques, be certain to specify each condition that controls the looping process. Poorly coded DO WHILE and DO UNTIL loops are major causes of never-ending loops. For example, if the EOF condition is not tested and department 'Q38' doesn't exist, the DO WHILE loop will loop endlessly. See Figure 7.11.