for loop groovy
Groovy For Loop
In Groovy, you can use the for
loop to iterate over a collection or a range of values. The syntax for a for
loop in Groovy is as follows:
for (item in collection) {
// code to be executed for each item
}
Here, item
is a variable that represents each element in the collection, and collection
is the collection or range of values to iterate over.
For example, let's say you have an array of numbers and you want to print each number using a for
loop:
def numbers = [1, 2, 3, 4, 5]
for (num in numbers) {
println(num)
}
This will output:
1
2
3
4
5
You can also use the for
loop with a range of values. For example, if you want to print numbers from 1 to 5:
for (i in 1..5) {
println(i)
}
This will output:
1
2
3
4
5
That's how you can use the for
loop in Groovy to iterate over collections or ranges of values.