Keyword

# For Each...Next

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Loops through the elements of a one-dimensional array or a class that implements the `Iterable</api/language/iterable>` interface.

## Usage

``` xojo
For Each element [ As DataType ] In array
  [ Statements ]
  [ Continue [ For ] ]
  [ Exit [ For ] ]
Next [ element ]
```

| Part                                     | Type                 | Description                                                                                                                                                                                                                                                                                         |
|------------------------------------------|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| element                                  | Same type as *array* | A variable of the same data type as *array* that refers to an element of *array*. The loop processes each value in *array*.                                                                                                                                                                         |
| *DataType*                               | Any valid datatype   | Optional: The data type of the array element.                                                                                                                                                                                                                                                       |
| array                                    | Any valid datatype   | A one-dimensional array whose data type must match *DataType*.                                                                                                                                                                                                                                      |
| Statements                               |                      | Statements to be executed repeatedly inside the loop.                                                                                                                                                                                                                                               |
| `Continue</api/language/loops/continue>` |                      | If a `Continue</api/language/loops/continue>` statement is present, execution skips directly to the loop's *Next* statement, thereby causing another loop iteration unless the end is reached.                                                                                                      |
| `Exit</api/language/loops/exit>`         |                      | If an `Exit</api/language/loops/exit>` statement is present, execution skips over the remaining statements in the loop and resumes with the statement following the *Next* statement. See the `Exit</api/language/loops/exit>` statement for additional options that are relevant for nested loops. |

## Notes

For...Each loops through the values in the array in index order.

As with other block statements, variables declared within a For...Each loop go out of scope when the loop finishes or exits.

<div class="warning">

<div class="title">

Warning

</div>

Modifying (adding to or deleting from) an array in a For...Each loop will invalidate the loop entirely.

</div>

## Sample code

Iterate through an array:

``` xojo
Var days() As String = Array("One", "Two", "Three", "Four", "Five")
Var output As String
For Each d As String In days
  output = output + d
Next
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Calculate the sum of values in an array:

``` xojo
Var values() As Double = Array(1.5, 5.5, 8.0, 45.0, 22.5)
Var sum As Double
For Each d As Double In values
  sum = sum + d
Next
' sum = 82.5
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`For...Next</api/language/loops/for...next>`, `Var</api/language/var>` statements; `Array</api/language/array>` keyword

</div>
