7 31 2 DOWHILE Examples

LANSA Technical

7.31.2 DOWHILE Examples

Executing a DOWHILE . . . ENDWHILE Routine

Executing DOWHILE . . . ENDWHILE to enter "n" records to a file

Comparing DOWHILE . . . ENDWHILE to the use of IF . . . GOTO . . . ENDIF

Executing DOWHILE . . . ENDWHILE routine with an Array Index

Executing a DOWHILE . . . ENDWHILE Routine

This is an example of the simple use of DOWHILE and ENDWHILE to count to 10 in a loop:

DEFINE     FIELD(#COUNT) REFFLD(#STD_NUM)
CHANGE     FIELD(#COUNT) TO(1)
DOWHILE    COND('#COUNT <= 10')
DISPLAY    FIELDS(#COUNT)
CHANGE     FIELD(#COUNT) TO('#COUNT + 1')
ENDWHILE
 

The DOWHILE command is similar in structure to the DOUNTIL command. However, there is one important difference. In the DOWHILE command, the condition is checked BEFORE doing the first iteration.

Executing DOWHILE . . . ENDWHILE to enter "n" records to a file

In this example, the details of 10 employees are inserted into a file:

GROUP_BY   NAME(#EMPDET) FIELDS(#EMPNO #SURNAME #SALARY)
DEFINE     FIELD(#COUNT) REFFLD(#STD_NUM)
DEF_LIST   NAME(#WORKER) FIELDS(#EMPNO #SURNAME #SALARY)
CHANGE     FIELD(#COUNT) TO(1)
DOWHILE    COND('#COUNT <= 10')
DISPLAY    FIELDS(#COUNT)
REQUEST    FIELDS(#EMPNO #SURNAME #SALARY)
ADD_ENTRY  TO_LIST(#WORKER)
CHANGE     FIELD(#COUNT) TO('#COUNT + 1')
ENDWHILE   
DISPLAY    BROWSELIST(#WORKER)
 

Using DEF_COND Values as DOWHILE parameters to make code easier to read and maintain

In this example, the COND parameter for the DOWHILE command is set by the DEF_COND command before DOWHILE is executed.

DEFINE     FIELD(#COUNT) REFFLD(#STD_NUM)
DEF_COND   NAME(*COUNT_TEN) COND('#COUNT <= 10')
CHANGE     FIELD(#COUNT) TO(1)
DOWHILE    COND(*COUNT_TEN)
DISPLAY    FIELDS(#COUNT)
CHANGE     FIELD(#COUNT) TO('#COUNT + 1')
ENDWHILE
 

The use of DEF_COND allows the programmer to give a complex condition a meaningful label that expresses the reason behind the test of the condition. When subsequent programmers read the DOWHILE statement, the meaningful label will help them to understand the purpose of the statement.

The use of DEF_COND also helps in situations where the same condition is referred to multiple times in the function. In this case it reduces the quantity of code and makes maintenance of the condition easier. For further details, refer to the DEF_COND command.

Comparing DOWHILE . . . ENDWHILE to the use of IF . . . GOTO . . . ENDIF

In this example, we see the simple use of the DOWHILE .... ENDWHILE routine:

 DOWHILE    COND('#A < B')
           << logic >>
           << logic >>
           << logic >>
ENDWHILE
 

Now compare this to the use of the IF .... GOTO .... ENDIF routine:

L01: IF         COND('#A < B')
           << logic >>
           << logic >>
           << logic >>
GOTO       LABEL(L01)
ENDIF
 

When compared, the use of the DOWHILE .... ENDWHILE routine is simpler and easier to read than when the IF .... GOTO .... ENDIF routine is used for a simple loop.

Executing DOWHILE . . . ENDWHILE routine with an Array Index

This example demonstrates the use of the DOWHILE .... ENDWHILE routine with an Array Index that groups 3 field values into an array, increments each one by 10%, then adds the resulting values up to display:

DEFINE     FIELD(#VAL1) REFFLD(#STD_NUM)
DEFINE     FIELD(#VAL2) REFFLD(#STD_NUM)
DEFINE     FIELD(#VAL3) REFFLD(#STD_NUM)
DEFINE     FIELD(#I1) REFFLD(#STD_NUM)
DEFINE     FIELD(#TOTAL) TYPE(*DEC) LENGTH(6) DECIMALS(2) LABEL(TOTAL) EDIT_CODE(3)
DEF_ARRAY  NAME(#ARR) INDEXES(#I1) OF_FIELDS(#VAL1 #VAL2 #VAL3)
CHANGE     FIELD(#TOTAL) TO(1)
CHANGE     FIELD(#I1) TO(1)
REQUEST    FIELDS(#VAL1 #VAL2 #VAL3)
DOWHILE    COND('#I1 <= 3')
CHANGE     FIELD(#ARR#I1) TO('#ARR#I1 * 1.1')
CHANGE     FIELD(#TOTAL) TO('#TOTAL + #ARR#I1')
CHANGE     FIELD(#I1) TO('#I1 + 1')
ENDWHILE   
DISPLAY FIELDS(#TOTAL)
 

Refer to the DEF_ARRAY command for further reference to the array index.

Refer to the 7.17 DEF_ARRAY command for further reference to the array index.