For in Command

Official Content
This documentation is valid for:

Allows traversing an array or a collection.


For &Var in expression


   Specifies a variable that must have the same data type as the items of the expression

   Any expression whose value is a collection, array or matrix.

   GeneXus code to be run in each iteration.


  • The array/matrix/collection values cannot be changed during the scanning. This means that changes in the &Var value, in the command scope, do not affect the corresponding array/matrix/collection or vice versa.
  • The vector/matrix/collection position cannot be obtained during the scanning. For this, a variable must be defined as counter.
  • These structures can be nested to scan several arrays, matrixes or collections. This takes into account when a Subroutine that also has a For In is called within the structure.
  • Like in a For Each command (X Evolution 2) or Do While, commands can be included that "break" the scanning, such as Exit or Return.


Suppose the expression is an array variable: 

for &var in &varArray()
   … // code

The previous code stores each value of the array &varArray in the &var variable. For vectors of one dimension, the code is expanded to:

&x = 1
Do while &x <= rows(&varArray())
   &var = &varArray(&x)
   ... // code
   &x += 1

 Expression can also be an array of two dimension (matrix) variables:

for &var in &varMatrix()
   ... // code

The expanded code would be as follows:

&x = 1
Do while &x <= rows(&varMatrix())
   &y = 1
    Do while &y <= cols(&varMatrix())
       &var = &varMatrix( &x, &y)
       ... // code
       &y += 1
  &x += 1

It may even a collection variable of any type (including a variable with Collection Property set as 'False' but of an 'SDT collection' data type). For more details, see the two ways of Implementing SDT collections:

for &var in &collection

An expression can not only be a variable, but also, for example, the result of a Data Provider or a Procedure:

for &var in DataProvider(parms)
for &var in Procedure(parms)

Remember not to write any method, 'Udp method' is assumed.

In sum, any expression whose evaluation results in a collection, array or matrix, is a valid one to run through.


  • Available for User Events in Android and iOS as of GeneXus 16 upgrade 3. In this case, Procedures and Data Providers cannot be used as part of the expression within the For in command. That is to say, when you are using the For in command in User Events on the Android and iOS platforms, you cannot use the output of a Procedure or a Data Provider as an expression.

See Also

Collection variables
Collection Domains

Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.