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.
| Modifiers | Name | Description |
|---|---|---|
static Parameter |
FOR_LOOP_DUMMY |
| 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. |
| 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) |
| 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 |
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
indexVariable
- the optional loop index Parameter, or null if no index is neededvalueVariable
- the Parameter representing the loop value; must not be nullcollectionExpression
- the Expression that produces the iterable or collection to loop over; must not be nullloopBlock
- the Statement to execute for each iteration; must not be nullConstructs 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
valueVariable
- the Parameter representing the loop value; must not be nullcollectionExpression
- the Expression that produces the iterable or collection to loop over; must not be nullloopBlock
- the Statement to execute for each iteration; must not be nullConstructs 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++) { ... }
closureListExpression
- the ClosureListExpression containing loop initialization, condition, and update; must not be nullloopBlock
- the Statement to execute for each iteration; must not be nullReturns 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 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 value Parameter for for-in loops, or null if this is a classic for loop (represented internally with a dummy parameter).
Returns the VariableScope associated with this for loop, containing loop variable bindings and type information.
Sets the Expression that produces the collection or iterable to loop over.
collectionExpression
- the Expression that evaluates to an iterable; must not be nullSets the Statement to execute for each loop iteration.
loopBlock
- the loop body Statement; must not be nullSets the VariableScope for this loop.
scope
- the VariableScope to associate with this loopCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.