Looping

In this section:

How to:

The Dialogue Manager command -REPEAT allows looping in a procedure.


Top of page

x
Syntax: How to Use the -REPEAT Command
-REPEAT label {n TIMES         [FROM fromval] [TO toval] [STEP s]}
-REPEAT label {WHILE condition [FROM fromval] [TO toval] [STEP s]}
-REPEAT label {FOR &variable   [FROM fromval] [TO toval] [STEP s]}

where:

label

Identifies the code to be repeated (the loop). A label includes another loop if the label for the second loop has a different name from the first.

n TIMES

Specifies the number of times to execute the loop. The value of n is a local variable, a global variable, or a constant. If it is a variable, it is evaluated only once, so the only way to end the loop early is with -QUIT or -EXIT (the number of times to execute the loop cannot be changed).

WHILE condition

Specifies the condition under which to execute the loop. The condition is any logical expression that is either true or false. The loop is run if the condition is true.

FOR &variable

Is a variable that is tested at the start of each execution of the loop. It is compared with the value of fromval and toval (if supplied). The loop is executed only if &variable is less than or equal to toval (STEP is positive), or greater than or equal to toval (STEP is negative).

FROM fromval

Is a constant that is compared with &variable at the start of each execution of the loop. 1 is the default value.

TO toval

Is a value that is compared with &variable at the start of each execution of the loop. 1,000,000 is the default value.

STEP s

Is a constant used to increment &variable at the end of each execution of the loop. It may be positive or negative. 1 is the default value.

The parameters FROM, TO, and STEP appear in any order.


Top of page

x
Ending a Loop

A loop ends in one of three ways:

Note: If you later issue another -GOTO to return to the loop, the loop proceeds from the point where it left off.



Example: Using the -REPEAT Command

This section illustrates how to write each of the syntactical elements of -REPEAT.

  1. -REPEAT label n TIMES

    Example:

    -REPEAT LAB1 2 TIMES
    -TYPE INSIDE
    -LAB1 TYPE OUTSIDE

    The output is:

    INSIDE
    INSIDE
    OUTSIDE
  2. -REPEAT label WHILE condition

    Example:

    -SET &A = 1;
    -REPEAT LABEL WHILE &A LE 2;
    -TYPE &A
    -SET &A = &A + 1;
    -LABEL TYPE END: &A

    The output is:

    1
    2
    END: 3
  3. -REPEAT label FOR &variable FROM fromval TO toval STEP s

    Example:

    -REPEAT LABEL FOR &A FROM 1 TO 4 STEP 2
    -TYPE INSIDE &A
    -LABEL TYPE OUTSIDE &A

    The output is:

    INSIDE 1
    INSIDE 3
    OUTSIDE 5

iWay Software