Looping Structures [ Getting Started With TRANSACT V ] MPE/iX 5.0 Documentation
Getting Started With TRANSACT V
Looping Structures
In most of the examples, we have used LEVEL as the way to get a program
to loop. Transact also has the verbs REPEAT, UNTIL, and WHILE to control
looping.
For example, we could have written the last program using REPEAT as
follows:
_____________________________________________________________
| |
| 1 system ex16,base=orders(";"); |
| 2 list(auto) customer; |
| 3 data cust-no ("enter cust-no to delete"); |
| 4 if (cust-no) <> 0 |
| 5 then |
| 6 repeat |
| 7 do |
| 8 set(key) list (cust-no); |
| 9 get customer; |
| 10 display; |
| 11 input "delete this customer?"; |
| 12 if input = "Y","YES" |
| 13 then delete(current) customer |
| 14 else display "customer not deleted"; |
| 15 data cust-no ("enter cust-no to delete");|
| 16 doend |
| 17 until (cust-no) = 0; |
_____________________________________________________________
Figure 2-16. Program using REPEAT to loop
7 The DO verb designates the start of a block of code that is to be
executed under the control of the current verb, in this case the
REPEAT verb. The block of code is terminated with a DOEND verb.
This is line 16 in the example. Thus lines 8 through 15 are
executed under the control of the REPEAT statement.
DO/DOEND can also be used with the WHILE and IF verbs.
Replacing REPEAT with WHILE, the program looks like: WHILE statement"
_________________________________________________________
| |
| 1 system ex17,base=orders(";"); |
| 2 list(auto) customer; |
| 3 data cust-no ("enter cust-no to delete"); |
| 4 while (cust-no) <> 0 |
| 5 do |
| 6 set(key) list (cust-no); |
| 7 get customer; |
| 8 display; |
| 9 input "delete this customer?"; |
| 10 if input = "Y","YES" |
| 11 then delete(current) customer |
| 12 else display "customer not deleted"; |
| 13 data cust-no ("enter cust-no to delete");|
| 14 doend; |
_________________________________________________________
Figure 2-17. Program using WHILE to loop
It is easier programmatically to use the LEVEL verb. However, when we do
so, we ask the user to do a bit more in order to stop things. The user
must enter the character ] to get the loop to stop.
In order to use the simpler LEVEL structure and still keep things easy
for the user, we could put an extra test in our LEVEL program to
accomplish the same thing as we did using REPEAT or WHILE. The program
now looks like this.
__________________________________________________________
| |
| 1 system ex15a,base=orders(";"); |
| 2 set(delimiter) "/"; |
| 3 list(auto) customer; |
| 4 level; |
| 5 data cust-no ("enter cust-no to delete"); |
| 6 if (cust-no) |
| 0 |
| 7 then |
| 8 do |
| 9 set(key) list (cust-no); |
| 10 get customer; |
| 11 display; |
| 12 input "delete this customer?"; |
| 13 if input = "Y","YES" |
| 14 then delete(current) customer |
| 15 else display "customer not deleted";|
| 16 doend |
| 17 else |
| 18 end(level); |
__________________________________________________________
Figure 2-18. Program using LEVEL to loop
18 Programmatically, END(LEVEL) is the same as if the user entered
the special key ]. It terminates the current level of the
program.
The preceding three examples allow the user to get out of the loop by
just pressing [[RETURN]] in response to the cust-no prompt. That is,
each program continues to loop until the cust-no input is zero, which is
the result of pressing [[RETURN]] in response to the prompt for cust-no.
MPE/iX 5.0 Documentation