Structured For

Top  Previous  Next

×V3.0Ø

S

A

W

WS

ü

ü

In addition to the standard For loop structure supported by OLIE, EMUE also supports a Structured For loop that allows nesting.  Nesting is not permitted in the original For loop syntax.  The syntax that must be used when nesting loops or when using the structured format is as follows:

 

Syntax -

For <index> = <start> To <end> [Step <step>]

       one or more statements to process

Next <index>

 

Where:

<index> is a variable that contains an integer

       Note: the index variable does not need to be initialized ahead of time as it will automatically be assigned the value of <start>.

<start> is an integer or a variable containing an integer

<end> is an integer or a variable containing an integer

<step> is an integer or a variable containing an integer

 

Description:

This structure of the For command provides a lot of flexibility.

1.  The starting and ending index can be specified meaning that the index could start at 0, 1, or any other integer.

2.  A step value can be specified so that rather than incrementing the index by one after each iteration, it can be increased or decreased by any number <step>.  This number may be positive or negative.

3.  The keyword, CONTINUE, can be used to stop the current iteration early, and proceed on to the next one.

4.  The keyword, ExitFor, can be used to break out of a loop early.

5.  For loops can be nested within one another.

6.  The index value and the step value may be a negative number.

 

Note: In order to create a loop that continues until an ExitFor is encountered, set the step to 0 so that the starting index is never incremented, which means that the ending index will never be reached.

 

Sample 1:

       The following is an example of a loop that starts at 5 and runs until row is equal to nineteen.

For row = 5 to 19

'code to be executed

Next row

 

Sample 2:

       The following is an example of a loop that starts at 5 and runs until Row is equal to nineteen.  The row variable is incremented by two for each iteration of the loop.

For Row = 5 to 19 Step 2

       'code to be executed

Next Row

 

Sample 3:

       The next example shows one loop nested inside of another loop.

For i = 0 to 5

       For j = 1 to 3

               'code to be executed

       Next j

       'code to be executed

Next i

 

Sample 4:

       The following example uses a continue statement to move on to the next iteration without running the final bit of code in the loop.

For i = 0 to 10

       If i = 3 Then

               Continue

       EndIf

       'code to be executed.  This code is not executed when i equals 3.

Next i

 

Sample 5:

The following example iterates through rows on the mainframe looking for specific text - in this case, an account number.  For each iteration, text is copied from the screen and compared to the desired text.  Once the desired text is found, the ExitFor statement is used to exit the loop.

For Row = 1 to 24

       Copy AccountNoFromScreen Screen Row 5 9

       If AccountNoFromScreen eq AccountNoFromFile Then

               Copy FoundAccount "Yes"

               ExitFor

       EndIf

Next Row