# For Each...End For Each

> Kalipso Studio 6.1 documentation · https://doc.sysdevmobile.com/kalipso/6.1/documentation/actions/group-code-flow/for-each...end-for-each
>
> Loops with For Each over a table control, Select or SQL Advanced results, a stored procedure, string tokens, XML children, a JSON array or a counter.

**For Each...End For Each**

For each is an action for traversing items in a collection. For each is usually used in place of a standard for statement Unlike other for loop constructs, however, for each loops usually maintain no explicit counter, they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read.\
\
This action have the following options: _Browse a Table Control_, _Browse the result of a Stored Procedure_, _Browse a Result of a Select Statement_, _Browse a SQL Advanced_, _Browse the tokens of a String_, _Browse the Childs of a XML, For_ and _Browse a JSON Array_.

Action available for the following operating systems:

![OS Supported: Android, iOS, Win, Win32](https://doc.sysdevmobile.com/kalipso/assets/images/01_Supported_OS-5c02a51fd5080a410a07ee3ef72b6872.png)

## Parameters

When you use this action, a window opens that asks you to select the object you want to browse. Choosing the desired option click _next_.

_Browse a Table Control:_

* **Table Control** _\<unquoted string> -_ The name of the table control that the user wants to browse.
* **Target** _\<unquoted string>_ - The control or variable that saves the value of each for iteration.

_Browse the result of a Stored Procedure:_

* The parameters of that option are the same of [_Exec. Procedure_](https://doc.sysdevmobile.com/kalipso/6.1/documentation/actions/group-database/exec.-procedure.md) action.

_Browse a Result of a Select Statement:_

* The parameters of that option are the same of [_Select_ ](https://doc.sysdevmobile.com/kalipso/6.1/documentation/actions/group-database/select.md)action.

_Browse a SQL Advanced:_

* The parameters of that option are the same of [_SQL Advanced_](https://doc.sysdevmobile.com/kalipso/6.1/documentation/actions/group-database/sql-advanced.md) action.

_Browse the tokens of a String:_

* **Source String** _\<string>_ - This parameter is the string that the user wants to browse.
* **String Separator** _\<string>_ - This parameter is the string separator for the source string.
* **Target For Each Token** _\<string> -_ The control or variable to save each token.

_Browse the Childs of a XML:_

* **Source XML** _\<string>_ - The complete path and name of the source XML file.
* **Child's Name** _\<string> -_ The name of the XML child that the user wants to browse.
* **Ignore Name´s Prefix** _\<combobox> -_ This parameter indicates that if the user wants or not to ignore the prefix of each name. This parameter have three possible values: _Yes_, _No_, _dynamic_.
* **Content's Target** _\<string>_ or _\<numeric>_ - The control or variable that saves the result of each iteration.
* **Attribute** _\<string> -_ The name of the XML attribute.
* **Target** _\<string>_ or _\<numeric> -_ The control or variable that saves the XML attribute.

_For:_

* **Type** _\<string>_ or _\<numeric> -_ The type of the _step_ and _initial value_ of the _for_ action.
* **Enumerator** _\<string>_ or _\<numeric> -_ Object used to control the iteration.
* **Initial Value** _\<string>_ or _\<numeric> -_ Expression used to initialize the enumerator.
* **While** _\<unquoted string> -_ Condition to continue the loop.
* **Step** _\<unquoted string> -_ Expression to set the value of Enumerator in each iteration.

_Browse a JSON Array:_

* **Source JSON** <_string_> - Enter the JSON source with the array to iterate on.
* **Target of Value** - Pick a Variable or Control that will receive each item of the array.
* **Target of Type** - Define the target variable that will receive the type of item inside the array while it is being iterated. It can be one of:
  * String
  * Numeric
  * Object
  * Array
  * Boolean
  * Null

## Usage

Consider the following information:

| Code | Name       | Stock |
| ---- | ---------- | ----- |
| 1    | Coca Cola  | 100   |
| 2    | Pepsi      | 200   |
| 3    | Pork Shops | 300   |

### **Example 1**

_Browse a Table Control:_

```
For Each(Table Control; CTRL(TableExample); VAR(0))

End For Each
```

**Result:**

| **Iteration** | **Variable** | **Value** |
| ------------- | ------------ | --------- |
| 1             | VAR(0)       | 1         |
| 2             | VAR(0)       | 2         |
| 3             | VAR(0)       | 3         |

Note that in each iteration, VAR(0) stores the value of line index of the table contro&#x6C;_._

**Example 2**

_Browse the tokens of a String:_

```
Set Value(VAR(0); String; "Kalipso is a tool to develop mobile applications.")

For Each(Token of String; VAR(0); " "; VAR(1))

End For Each
```

**Result:**

| **Iteration** | **Variable** | **Value**      |
| ------------- | ------------ | -------------- |
| 1             | VAR(1)       | "kalipso"      |
| 2             | VAR(1)       | "is"           |
| 3             | VAR(1)       | "a"            |
| 4             | VAR(1)       | "tool"         |
| 5             | VAR(1)       | "to"           |
| 6             | VAR(1)       | "develop"      |
| 7             | VAR(1)       | "mobile"       |
| 8             | VAR(1)       | "applications" |

### Example 3

_For:_

```
Set Value(VAR(0); Numeric; 0)

For Each(FOR; Numeric; VAR(0); 1; VAR(0) <= 5; VAR(0) + 1)

Set Value(VAR(1); Numeric; VAR(1) + 1)

End For Each
```

**Result**:

| **Variable** | **Value** |
| ------------ | --------- |
| VAR(0)       | 6         |
| VAR(1)       | 5         |

### Example _4_

_For:_

```
Set Value(VAR(0); String; "")

For Each(FOR; String; VAR(0); ""; Length(VAR(0)) < 10; VAR(0) + "a")

Set Value(VAR(1); Numeric; VAR(1) + 1)

End For Each
```

**Result:**

| **Variable** | **Value**    |
| ------------ | ------------ |
| VAR(0)       | "aaaaaaaaaa" |
| VAR(1)       | 10           |
