Page History
7.40.2 DOWHILE Examples
Executing a DOWHILE . . . ENDWHILE Routine
Executing DOWHILE . . . ENDWHILE to enter "n" records to a file
Using DEF_COND Values as DOWHILE parameters to make code easier to read and maintain
Comparing DOWHILE . . . ENDWHILE to the use of IF . . . GOTO . . . ENDIF
Executing DOWHILE . . . ENDWHILE routine with an Array Index
| Anchor | ||||
|---|---|---|---|---|
|
This is an example of the simple use of DOWHILE and ENDWHILE to count to 10 in a loop:
...
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.
| Anchor | ||||
|---|---|---|---|---|
|
In this example, the details of 10 employees are inserted into a file:
| Code Block |
|---|
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) |
| Anchor | ||||
|---|---|---|---|---|
|
In this example, the COND parameter for the DOWHILE command is set by the DEF_COND command before DOWHILE is executed.
...
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 7.22 DEF_COND command.
| Anchor | ||||
|---|---|---|---|---|
|
In this example, we see the simple use of the DOWHILE .... ENDWHILE routine:
...
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.
| Anchor | ||||
|---|---|---|---|---|
|
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:
| Code Block |
|---|
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 7.20 DEF_ARRAY command for further reference to the array index.