public class ForStatement
extends Statement
implements LoopingStatement
Represents a for loop in Groovy.
| Modifiers | Name | Description |
|---|---|---|
static Parameter |
FOR_LOOP_DUMMY |
| Constructor and description |
|---|
ForStatement(Parameter indexVariable, Parameter valueVariable, Expression collectionExpression, Statement loopBlock)A constructor of for-in loops (possibly with an optional index variable). |
ForStatement(Parameter valueVariable, Expression collectionExpression, Statement loopBlock)A constructor of for-in loops without an index variable. |
ForStatement(ClosureListExpression closureListExpression, Statement loopBlock)A constructor of classic (C-style) for loops. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public Expression |
getCollectionExpression()Retrieves the collection expression of a for loop. |
|
public Parameter |
getIndexVariable()Retrieves the index variable of for-in loops with an index variable or null in other cases. |
|
public Statement |
getLoopBlock() |
|
public Parameter |
getValueVariable()Retrieves the value variable of for-in loops or null for a classic for loop. |
|
public Parameter |
getVariable() |
|
public VariableScope |
getVariableScope() |
|
public ClassNode |
getVariableType() |
|
public void |
setCollectionExpression(Expression collectionExpression) |
|
public void |
setLoopBlock(Statement loopBlock) |
|
public void |
setVariableScope(VariableScope scope) |
|
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 |
A constructor of for-in loops (possibly with an optional index variable). An example with an index variable:
for (int idx, int i in 10..12) {
println "$idx $i"
}
A constructor of for-in loops without an index variable. Variants using Java-style ":" or the Groovy reserved word "in" are equivalent:
for (int i : 0..2) {
println i
}
for (int j in 5..7) {
println j
}
A constructor of classic (C-style) for loops.
for (int i = 20; i < 23; i++) {
println i
}
Also handles multi-assignment for loops.
for (def (String i, int j) = ['a', 30]; j < 33; i++, j++) {
println "$i $j"
}
Retrieves the collection expression of a for loop. Will be an instance of ClosureListExpression for a classic for loop.
Retrieves the index variable of for-in loops with an index variable or null in other cases.
Retrieves the value variable of for-in loops or null for a classic for loop.
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.