Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Groovy - Loops: For, While, Each

7. Loops: For, While, Each

Loops in programming are used to execute a block of code repeatedly. In Groovy, you can use 'for', 'while', and 'each' loops for various iteration tasks.

1. For Loops:

For loops are used when you know in advance how many times you want to execute a block of code. Groovy provides a concise way to write for loops using the 'in' keyword.

Syntax:
for (element in collection) {
    // Code to execute for each element in the collection
}
 
Example:
def numbers = [1, 2, 3, 4, 5]

for (num in numbers) {
    println("Number: ${num}")
}
 

In this example, the for loop iterates through each element in the 'numbers' list and prints them.

2. While Loops:

While loops are used when you want to execute a block of code repeatedly as long as a specified condition is true.

Syntax:
while (condition) {
    // Code to execute as long as the condition is true
}
 
Example:
def count = 1

while (count <= 5) {
    println("Count: ${count}")
    count++
}
 

In this example, the while loop prints the value of  'count' as long as it is less than or equal to 5.

3. Each Loops:

Each loops are used to iterate over collections, such as lists and maps, in a concise way. Groovy provides a built-in 'each' method for this purpose.

Syntax (for Lists):
collection.each { element ->
    // Code to execute for each element in the collection
}
 
Syntax (for Maps):
map.each { key, value ->
    // Code to execute for each key-value pair in the map
}
 
Example (for Lists):
def colors = ['red', 'green', 'blue']

colors.each { color ->
    println("Color: ${color}")
}
 

In this example, the 'each' loop iterates through each element in the 'colors'list and prints them.

Example (for Maps):
def person = [name: 'John', age: 25]

person.each { key, value ->
    println("Key: ${key}, Value: ${value}")
}
 

In this example, the 'each' loop iterates through each key-value pair in the 'person' map and prints them.

Break and Continue:

You can use the 'break' statement to exit a loop prematurely and the 'continue' statement to skip the current iteration and proceed to the next one.

Example (Using Break):
def numbers = [1, 2, 3, 4, 5]

for (num in numbers) {
    if (num == 3) {
        break
    }
    println("Number: ${num}")
}
 

In this example, the loop will break when it encounters the value 3, and no further iterations will occur.

Example (Using Continue):
def numbers = [1, 2, 3, 4, 5]

for (num in numbers) {
    if (num == 3) {
        continue
    }
    println("Number: ${num}")
}
 

In this example, the loop will skip the iteration when it encounters the value 3, and the remaining numbers will be printed.

Loops are essential for performing repetitive tasks in programming. Whether you know the exact number of iterations (for loops), need to continue until a condition is met (while loops), or want to iterate over collections (each loops), understanding and using these constructs effectively is crucial for writing efficient Groovy code.