Previous Topic: Add REPEAT UNTIL ConditionNext Topic: Add Control Actions


Add FOR Condition

The FOR Action Diagram statement provides a concise way to set up a program loop. It is often used to control iterating through an explicitly indexed repeating group view. The syntax allows you to pick a subscript or local numeric attribute view, a starting value, an ending or limit value, and an increment value for each iteration.

The FOR Action Diagram statement is similar to the FOR statement that is provided by many programming languages. However, one major difference is that any changes to the limit and increment values during iteration of the FOR loop will not affect the actual incrementation or limit testing that is done. This is because these values are saved in work variables before the loop begins, and those work variables are used for each iteration to avoid re-evaluating the limit and increment expressions each time.

Another difference between the FOR Action Diagram statement and a typical FOR statement in a programming language is that, if the increment value in a FOR Action Diagram statement is negative, the loop will terminate when the control view or subscript is less than or equal to the limit or ending value. Otherwise, the loop will terminate when the control view or subscript is greater than or equal to the limit or ending value.

In the following example, the FOR action statement starts a loop that repeatedly executes the IF statement that follows:

FOR local work_items counter FROM 1 TO 10 BY 1
IF input customer_order confirmed_date IS LESS OR EQUAL TO CURRENT_DATE + local work_items counter DAYS
EXIT STATE IS confirm_date_order
<--ESCAPE

The first time through the loop, the IF statement is executed with a value of 1. If the conditions are met, the local counter increments by 1 and the entire set of statements executes again. The process repeats 10 times. If the conditions are not met (input customer_order confirmed_date is greater than the current date plus the number of days indicated by the counter), the exit state is set and the FOR loop is terminated by the ESCAPE statement.

The following FOR statement example shows how a reference to the subscript of an explicitly indexed repeating group view appears in the Action Diagram:

FOR SUBSCRIPT OF group_customer_order
FROM 1 TO MAX OF group_ customer_order BY 1

The following example of a fully defined FOR statement uses an explicitly indexed repeating group view. The statement shows how subscripts are used both to control the FOR loop and to calculate totals.

FOR SUBSCRIPT OF loan_payments FROM 1 TO 360 BY 1
SET payment principle TO (loan principle amount * table factor)
SET payment total TO payment principle + payment interest
SET local work_view yearly_total TO local work_view
yearly_total + payment total
IF local work_view month_counter IS EQUAL TO 12
SET yearly payment total TO local work view yearly_total
SET local work_view month_counter TO 1
SET SUBSCRIPT OF yearly_totals TO yearly totals + 1
ELSE
SET local work_view month_counter TO 1

The following example shows how expressions may be used as the initial, increment, and limit values:

FOR local employee fica_withheld_ytd FROM (employee salary * employee fica_rate) TO (employee salary * 12 * employee annual_fica_max_rate) BY (employee salary * employee fica_rate)
IF local employee fica_withheld_ytd IS GREATER THAN (employee salary * 12 * employee annual_fica_max_rate)
SET local employee fica_withheld_ytd TO (employee salary * 12 * employee annual_fica_max_rate)

In the preceding example, the loop will terminate when the employee_salary * 12 * employee annual_fica_max_rate value is reached. The enclosed IF block prevents local employee fica_withheld_ytd from exceeding it.

The following example shows that a change to the limit or increment during execution of the FOR loop will not affect the values actually used during execution of the FOR statement:

SET local work_items limit TO 10
FOR local work_items counter FROM 1 TO local work_items limit BY 1
SET local work_items limit TO 5

When the preceding FOR statement terminates, local work_items counter will have a value of 10, not 5, even though the limit variable was reset during the loop. This is because the values of the limit and increment expressions are saved in work variables before the first FOR iteration, and the saved values are the ones actually used during the execution of the statement.