FOR EACH

FOR EACH

Executes a block of statements for a data type containing multiple elements.

Syntax

      FOR EACH var1 [,var255] IN expr1 [,expr255] [DESCEND]
          [EXIT]
          [LOOP]
          ...
      NEXT

Arguments

– expr can be a string or an array – enumerator variable ‘var<n>’ stores a reference to the element of an array or a string specified by ‘expr<n>’ thus assigments to the enumerator changes the value of given array element

Description

FOR EACH is a support to enumeration / iteration loop slightly different than FOR…NEXT loop. This is a control structure that executes a block of statements for a data type containing multiple elements ( a string or an array ).

Note:

– after the loop the controlling variable(s) store the value which they had before entering the loop – the enumerator variable supports the following properties :

:__enumindex – the loop counter for variable :__enumbase – the value that is being traversed

:__enumvalue – the value of variable

Example

       a := 'A'
       b := 'B'
       FOR EACH a, b IN { 1, 2, 3, 4 }, "abcd"
          ? a, b   //prints: 1 a
                   //        2 b
                   //        3 c
                   //        4 d
       NEXT
       ? a, b   //prints: A B
       // you can use EXIT statement inside the loop
       FOR EACH a IN { 1, 2, 3, 4 }
          IF a:__enumindex == 3
             ? a
             EXIT
          ENDIF
       NEXT
       arr := { 1, 2, 3 }
       str := "abc"
       FOR EACH a, b IN arr, str
          a *= 2
          str := Upper( str )
       NEXT
       // now 'arr' stores { 2, 4, 6 }
       // howerer 'str' still stores "abc"

Notice the difference:

       FOR EACH a IN someValue 
           ? a:__enumindex   // prints current value of the index 
           ? (a):__enumindex // sends '__enumindex' message to the current value 
       NEXT
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s