Package: org.codehaus.groovy.ast.stmt

## [Java] Class ForStatement

- org.codehaus.groovy.ast.stmt.ForStatement

- All Implemented Interfaces and Traits:
    LoopingStatement

      public class ForStatement
      extends Statement
      implements LoopingStatement

  Represents a for loop in Groovy, supporting both for-in loops (with values and optional indices) and classic (C-style) for loops with initialization, condition, and update expressions. For-in loops iterate over a collection or iterable expression, optionally capturing the index. Classic for loops use a ClosureListExpression to represent initialization, condition, and update expressions and may include multi-assignment declarations.

  See Also:
    LoopingStatement
    Statement
    Parameter
    Expression
    ClosureListExpression

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `static Parameter` | `FOR_LOOP_DUMMY` |

  - ### Constructor Summary

    Constructors
    Constructor and description
    `ForStatement(Parameter indexVariable, Parameter valueVariable, Expression collectionExpression, Statement loopBlock)` Constructs a for-in loop with an optional index variable and a value variable.
    `ForStatement(Parameter valueVariable, Expression collectionExpression, Statement loopBlock)` Constructs a for-in loop with a value variable but no index.
    `ForStatement(ClosureListExpression closureListExpression, Statement loopBlock)` Constructs a classic (C-style) for loop with optional multi-assignment support.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public Expression` | `getCollectionExpression()` Returns the Expression that produces the iterable to loop over.
    `` | `public Parameter` | `getIndexVariable()` Returns the optional index Parameter for for-in loops with an index variable, or null if this is a for-in loop without an index or a classic for loop.
    `` | `public Statement` | `getLoopBlock()` Returns the loop body Statement.
    `` | `public Parameter` | `getValueVariable()` Returns the value Parameter for for-in loops, or null if this is a classic for loop (represented internally with a dummy parameter).
    `` | `public Parameter` | `getVariable()`
    `` | `public VariableScope` | `getVariableScope()` Returns the VariableScope associated with this for loop, containing loop variable bindings and type information.
    `` | `public ClassNode` | `getVariableType()`
    `` | `public void` | `setCollectionExpression(Expression collectionExpression)` Sets the Expression that produces the collection or iterable to loop over.
    `` | `public void` | `setLoopBlock(Statement loopBlock)` Sets the Statement to execute for each loop iteration.
    `` | `public void` | `setVariableScope(VariableScope scope)` Sets the VariableScope for this loop.
    `` | `public void` | `visit(GroovyCodeVisitor visitor)`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Statement` | `addStatementAnnotation, addStatementLabel, copyStatementLabels, getStatementAnnotations, getStatementLabel, getStatementLabels, isEmpty, setStatementLabel`
    `class ASTNode` | `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit`

- - ### Field Detail

    - #### @Deprecated(since = "6.0.0")
      public static final Parameter FOR_LOOP_DUMMY

  - ### Constructor Detail

    - #### public ForStatement(Parameter indexVariable, Parameter valueVariable, Expression collectionExpression, Statement loopBlock)

      Constructs a for-in loop with an optional index variable and a value variable. This constructor supports both indexed and non-indexed iteration patterns:

           for (int i in 10..12) { ... }                    // no index
           for (int idx, int i in 10..12) { ... }           // with index

      throws:
        NullPointerException if valueVariable, collectionExpression, or loopBlock is null
      Parameters:
        `indexVariable` - the optional loop index Parameter, or null if no index is needed
        `valueVariable` - the Parameter representing the loop value; must not be null
        `collectionExpression` - the Expression that produces the iterable or collection to loop over; must not be null
        `loopBlock` - the Statement to execute for each iteration; must not be null
      Since:
        5.0.0

    - #### public ForStatement(Parameter valueVariable, Expression collectionExpression, Statement loopBlock)

      Constructs a for-in loop with a value variable but no index. This is the most common for-in loop pattern, equivalent to Java's enhanced for loop:

           for (int i : 0..2) { ... }     // Java-style colon syntax
           for (int j in 5..7) { ... }    // Groovy-style 'in' keyword

      throws:
        NullPointerException if valueVariable, collectionExpression, or loopBlock is null
      Parameters:
        `valueVariable` - the Parameter representing the loop value; must not be null
        `collectionExpression` - the Expression that produces the iterable or collection to loop over; must not be null
        `loopBlock` - the Statement to execute for each iteration; must not be null
      Since:
        5.0.0

    - #### public ForStatement(ClosureListExpression closureListExpression, Statement loopBlock)

      Constructs a classic (C-style) for loop with optional multi-assignment support. The ClosureListExpression encapsulates initialization, condition, and update expressions:

           for (int i = 20; i < 23; i++) { ... }
           for (def (String i, int j) = ['a', 30]; j < 33; i++, j++) { ... }

      throws:
        NullPointerException if closureListExpression or loopBlock is null
      Parameters:
        `closureListExpression` - the ClosureListExpression containing loop initialization, condition, and update; must not be null
        `loopBlock` - the Statement to execute for each iteration; must not be null
      Since:
        6.0.0

  - ### Method Detail

    - #### public Expression getCollectionExpression()

      Returns the Expression that produces the iterable to loop over. For classic for loops, this is a ClosureListExpression containing initialization, condition, and update expressions.

      Returns:
        the collection or control Expression

    - #### public Parameter getIndexVariable()

      Returns the optional index Parameter for for-in loops with an index variable, or null if this is a for-in loop without an index or a classic for loop.

      Returns:
        the index Parameter, or null
      Since:
        5.0.0

    - #### public Statement getLoopBlock()

      Returns the loop body Statement.

      Returns:
        the loop block Statement

    - #### public Parameter getValueVariable()

      Returns the value Parameter for for-in loops, or null if this is a classic for loop (represented internally with a dummy parameter).

      Returns:
        the value Parameter, or null for classic for loops
      Since:
        5.0.0

    - #### @Deprecated(since = "5.0.0")
      public Parameter getVariable()

    - #### public VariableScope getVariableScope()

      Returns the VariableScope associated with this for loop, containing loop variable bindings and type information.

      Returns:
        the VariableScope}

    - #### @Deprecated(since = "5.0.0")
      public ClassNode getVariableType()

    - #### public void setCollectionExpression(Expression collectionExpression)

      Sets the Expression that produces the collection or iterable to loop over.

      throws:
        NullPointerException if collectionExpression is null
      Parameters:
        `collectionExpression` - the Expression that evaluates to an iterable; must not be null

    - #### public void setLoopBlock(Statement loopBlock)

      Sets the Statement to execute for each loop iteration.

      throws:
        NullPointerException if loopBlock is null
      Parameters:
        `loopBlock` - the loop body Statement; must not be null

    - #### public void setVariableScope(VariableScope scope)

      Sets the VariableScope for this loop.

      Parameters:
        `scope` - the VariableScope to associate with this loop

    - #### public void visit(GroovyCodeVisitor visitor)