Start/Repeat LOOPS

PURPOSE:  A START/REPEAT loop is used to repeatedly execute a group of statements within a process section.  START/REPEAT loops follow a structured programming design, which is easier to understand and maintain than the conventional IF…GOTO statements.

SYNTAX

START[:n] [FOR \\fieldn = begn TO endn [BY incrn]\\

PM statement(s)

[LEAVE[:n] [IF; UNLESS clause]]

[PM statement(s)]

REPEAT[:n]

:n

is a positive integer between 0 and 99999, indicating the level of the given control statement.  If omitted, ACCENT R assumes level 0.

START

specifies the beginning of a REPEAT block with the indicated level number.

FOR fieldn

must be an integer, real, or numeric field defined in the DECLARE section of the PM.  The FOR (iteration) clause specifies how many times the loop is to be executed.

Using a binary field substantially improves processing efficiency.  If symbolic fields are specified with USAGE IS ASCII, a warning message is displayed when the PM is saved that these fields will retard execution, but the PM will still be saved and will execute.

begn

is an expression specifying the beginning inclusive range of fieldn.

endn

is an expression specifying the ending inclusive range of fieldn.

BY incrn

is the amount by which fieldn increases or decreases.  If BY incrn is omitted, ACCENT R assumes the incrementing value is 1.  The incrn can be negative, in which case the beginning value is the larger and the ending value is the smaller.  Example:

FOR LOP I = 10 TO 1 BY -1

Each loop is iterated within the scope of the loop defined immediately before it. The values of begn, endn, and incrn are calculated at the start of iteration, and one iteration will always occur with the value of begn.  Each loop specification is separated by a comma.

LEAVE

exits the loop defined by the START and REPEAT at that level.  A START/REPEAT loop must have at least one LEAVE statement if the START statement does not contain a FOR clause.  It can have several LEAVE statements.

REPEAT

specifies the end of the REPEAT block with the indicated level number.  It returns control to the beginning of the loop.

Example

A START statement initializes a loop.  It appears at the beginning of each loop.  An example of a START statement is:

START FOR MO = 1 TO @MONTH
TYPE "ENTER ORDER QUANTITY FOR MONTH",N
ACCEPT QTY
QTY*PRICE(MO)+YTD_SALES TO YTD_SALES
REPEAT

The START statement also terminates any preceding nest or loop at the same or lower level.

The LEAVE statement permits early termination of a START/REPEAT loop with a for clause.  The loop is used in the example below to perform the same statements on each of the 12 elements in the sales field (defined as SALES, FLOAT,7,2,OCCURS 12 from an unspecified Data Set).

DECLARE SECTION
GROSS,FLOAT,8,2
CNT,INT,2
INITIAL SECTION
TYPE "GROSS SALES"
DETAILS SECTION
START FOR CNT:D=1 TO 12
LEAVE IF CNT:D GT @MONTH
GROSS:D+SALES:M(CNT:D) TO GROSS:D
REPEAT
TYPE "GROSS SALES ARE ",GROSS:D

In the example above, ACCENT R may repeat the START/REPEAT loop as many as 12 times.  ACCENT R ends the loop if the value in CNT is greater than the current month.  If CNT is less than the current month, ACCENT R executes the specified operation to update the declared field GROSS.  The REPEAT statement instructs ACCENT R to return to the beginning of the loop unless the loop has been executed 12 times.

Another way to do the same operation is shown in the example below except it will execute the loop 12 times but not add the sales amount when CNT is greater than the current month.

DECLARE SECTION
GROSS, FLOAT, 8, 2
CNT, INT, 2
INITIAL SECTION
TYPE "GROSS SALES"
DETAILS SECTION
ADD SALES:M(CNT:D) TO GROSS:D UNLESS CNT:D GT @MONTH &
FOR CNT:D = 1 TO 12
TYPE "GROSS SALES ARE ",GROSS:D

Another common use of START/REPEAT loops is the validation of data entered with an ACCEPT statement.  These loop statements will continue to prompt for the quantity until an allowable value is entered.

START
TYPE "ENTER ORDER QUANTITY:",NOCR
ACCEPT QTY
LEAVE IF QTY # 0
TYPE "ERROR, QUANTITY OF ZERO IS NOT VALID”
REPEAT

NOTES:  If the FOR (iteration) clause is used, the LEAVE statement is optional.  The iterate clause can contain any number of iteration loops, which facilitates use of multiple occurrence fields, array processing, and output to multiple report files.  See the description of the FOR cause at the beginning of this chapter.

A START/REPEAT loop may contain other loops or block control statements.  START/REPEAT loops are nested in the same way that control statements are nested.  Level numbers are used to indicate the position of each loop.  For example, it is possible to have the following structure:

START
START:10
LEAVE:10
REPEAT:10
LEAVE
REPEAT

Here the loop identified with the level number 10 is contained in a level 0 (zero) loop.

If a loop contains a nest of control statements, the control statements must have a higher number. Note that nests and loops at higher levels always terminate lower level nests and loops.  Two possible nests are shown below.  The one on the left is valid.  The one on the right is not.

Valid Nesting

Invalid Nesting

START

IF:10

START:30

LEAVE:30

REPEAT:30

ELSE:10

CONTINUE:10

LEAVE

REPEAT

START

IF

ELSE

CONTINUE

REPEAT

 

(The if is at the same

level as the START)