Control Statements
Groovy control statements are modeled on C-style statements. You can do a lot with very little.
Branching Logic
Groovy support if/else statements, switch statements and the ternary operator. Generally you should be able to get by with using if/else statements.
If Else Statements
The if else statements are pretty straightforward.
def seats = 2
if (seats == 0) {
println 'There are no seats'
} else if (seats == 1) {
println 'There is one seat'
} else if (seats == 2) {
println 'There are two seats'
} else {
println 'There are more than two seats, if you think positively'
}
which will print out the message
There are two seats
Compact If Else plus Assignment
There is also the ternary operator, which provides an if else statement combined with a variable assignment.
def seats = 1
def msg = (seats == 2) ? "There are two seats left" : "Sorry we don't have 2 seats left"
println msg
This script will print out the message
Sorry we don't have 2 seats left
Looping
For Statement
Using the for statement you can loop over a range of numbers, lists and maps.
Number Iteration
In a for statement you can specify the starting index, the iteration test and increment operation.
def items = [ "banana", "apple", "orange", "mango", "grapes", "kiwi fruit" ]
// Start at index 0, only loop while the index is less than the length of the list,
// and increment the index by one for each iteration
for (int i = 0; i < items.size(); i++) {
println i + ". " + items[i]
}
This script will print out the message
0. banana
1. apple
2. orange
3. mango
4. grapes
5. kiwi fruit
List Iteration
When looping through a list you can use the for each item in items syntax
def items = [ "banana", "apple", "orange", "mango", "grapes", "kiwi fruit" ]
// For each item in the list of items
for (item in items) {
println item
}
This script will print out the message
banana
apple
orange
mango
grapes
kiwi fruit
Map Iteration
Maps are special in that you can iterate through them a number of ways.
def car = [ "seats" : 4, doors: 2, "color" : "red", "year" : 2011 ]
// For each map entry
for (entry in car) {
println entry.key + ": " + entry.value
}
def car = [ "seats" : 4, doors: 2, "color" : "red", "year" : 2011 ]
// For each map entry key (name)
for (key in car.keySet()) {
println key + ": " + car[key]
}
def car = [ "seats" : 4, doors: 2, "color" : "red", "year" : 2011 ]
// For each value in the map
for (value in car.values()) {
println value
}
Please note to find something in a map you can generally lookup the value using the key.
Returning Values
In Groovy script you can return an object to the calling code using the return statement.
def car = [ "seats" : 4, doors: 2, "color" : "red", "year" : 2011 ]
if (car["doors"] == 2) {
return "We don't insure sports cars"
}
println 'Continue processing'
// Continue processing
..
Catching Errors
If you need to handle errors and continue processing you can use the try / catch statement.
def textValue = "z123"
def intValue
try {
intValue = Integer.parseInt(textValue)
} catch (Exception e) {
println e
intValue = -1
}
println "Integer value: " + intValue