FOR EACH ... NEXT, ¿ Que es ? y ¿ Para que se usa ?

FOR EACH ... NEXT, ¿ Que es ? y ¿ Para que se usa ?

Postby JmGarcia » Mon Mar 31, 2008 4:50 pm

Code: Select all  Expand view  RUN
FOR EACH <element> IN <array>|<object>|<string>
   <statements>
   [LOOP]
   <statements>
   [EXIT]
NEXT

Debido a mi poca "sapiencia" del idioma ingles... alguien podría explicarme para que es y para que sirve la clausula FOR EACH ... NEXT

Algún ejejmplito seria interesante.

Gracias.
Mi abuelo decía: Los aviones vuelan porque Dios quiere, y los helicópteros ni Dios sabe porque vuelan.
FWH 16.02, xHarbour 1.2.3, Harbour 3.2.0, WorkShop 4.5, AJ Make 0.30, Borlan BCC 7.00, VisualStudio 2013
User avatar
JmGarcia
 
Posts: 654
Joined: Mon May 29, 2006 3:14 pm
Location: Madrid - ESPAÑA

Postby metaldrummer » Mon Mar 31, 2008 5:21 pm

No me di el tiempo de traducirlo. Espero esto te sirva.
FOR EACH
Iterates elements of data types that can be seen as a collection.
Syntax
FOR EACH <element> IN <array>|<object>|<string>
<statements>
[LOOP]
<statements>
[EXIT]
NEXT

Arguments
<element>
The name of a variable that gets assigned a new value on each iteration.
IN <array>
This is a value of data type Array. The FOR EACH loop iterates all array elements in the first dimension and assigns their values to <element>.
IN <object>
This is a value of data type Object. The FOR EACH loop iterates all instance variables of the object and assigns their values to <element>.
IN <string>
This is a value of data type Character string. The FOR EACH loop iterates all individual characters of the string and assigns them to <element>.
LOOP
The LOOP statement unconditionally branches to the FOR EACH statement, i.e. to the begin of the loop, where the next value is assigned to <element>.
EXIT
The EXIT statement unconditionally terminates the FOR EACH loop and branches to the statement following NEXT. Description
The FOR EACH statement forms a control structure that executes a block of statements for a data type containing multiple elements. This can be data of type Array, Object or Character string. The loop iterates all elements contained in the data and assigns the value of the next element to <element> on each iteration.
FOR EACH is similar to the regular FOR loop. But it completes considerably faster than a FOR loop, since there is no explicit loop counter. In contrast, FOR EACH uses an implicit loop counter whose value can be queried using the function HB_EnumIndex(). Other than this, LOOP and EXIT statements within the loop are treated the same as in a FOR loop.
When FOR EACH statements are nested, each loop maintains its own counter, i.e. HB_EnumIndex() retrieves the counter of the loop that is currently executed. When the FOR EACH loop is finished, its loop counter is set to 0.

Example
// The example demonstrates the FOR EACH statement using two
// nested loops. The outer loop iterates an array while the
// inner loop iterates character strings.

Code: Select all  Expand view  RUN
PROCEDURE Main()
      LOCAL aArray := { "Hello", "World" }
      LOCAL cString, cChar

      FOR EACH cString IN aArray
         ? "----- Outer loop -----"
         ? HB_EnumIndex(), cString

         ? "----- Inner loop -----"
         FOR EACH cChar IN cSTring
            ? HB_EnumIndex(), cChar
         NEXT
      NEXT

      ? "-------- End ---------"
      ? HB_EnumIndex()
   RETURN


/* Output of example:
----- Outer loop -----
1 Hello
----- Inner loop -----
1 H
2 e
3 l
4 l
5 o
----- Outer loop -----
2 World
----- Inner loop -----
1 W
2 o
3 r
4 l
5 d
-------- End ---------
0
*/

Saludos
[b]David Lagos S.
Coquimbo-Chile
www.wificafe.cl
webmaster@wificafe.cl[/b]
User avatar
metaldrummer
 
Posts: 113
Joined: Wed Jan 10, 2007 8:43 pm
Location: Coquimbo-Chile

Postby Antonio Linares » Mon Mar 31, 2008 8:33 pm

JM,

Te permite simplificar el recorrido por cada uno de los elementos de un array (ó de las DATAs de un objeto, ó de una cadena): No hay que usar una variable temporal ni usar el índice del elemento del array.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42159
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 35 guests