# Keyword Reference

# For...In...Next

Enumerates elements in an Object collection or an Array.

For <Variable> In <expression>
statements
...
Next

## Parameters

Variable | A variable to which an element is being assigned
expression | Must be an expression resulting in an Object, or an Array with at least one element

## Remarks

The Variable will be created automatically with a Local scope, even when MustDeclareVars is on.
If the expression is an Object collection with no elements, or an multi-dimensional array, the loop will be skipped and the Variable will contain an empty string.
If the expression is not an Object nor an Array, the script stops with an error, unless a COM Error handler had been configured.
AutoIt Arrays are read-only when using For...In. While you can assign the variable inside the For...In loop a value, this change is not reflected in the array itself. To modify the contents of an array during enumeration, use a For...To loop.
For...In...Next statements may be nested.

## Related

With...EndWith

## Example

    #include <MsgBoxConstants.au3>

    Example()

    Func Example()
        ; Using an Array
        Local $aArray[4]

        $aArray[0] = "a"
        $aArray[1] = 0
        $aArray[2] = 1.3434
        $aArray[3] = "test"

        Local $sString = ""
        For $vElement In $aArray
            $sString = $sString & $vElement & @CRLF
        Next

        MsgBox($MB_SYSTEMMODAL, "", "For..IN Arraytest:" & @CRLF & "Result is: " & @CRLF & $sString)

        ; Using an Object Collection

        Local $oShell = ObjCreate("shell.application")
        Local $oShellWindows = $oShell.windows

        If IsObj($oShellWindows) Then
            $sString = ""

            For $Window In $oShellWindows
                $sString = $sString & $Window.LocationName & @CRLF
            Next

            MsgBox($MB_SYSTEMMODAL, "", "You have the following windows open:" & @CRLF & $sString)
        Else

            MsgBox($MB_SYSTEMMODAL, "", "You have no open shell windows.")
        EndIf
    EndFunc   ;==>Example